CodeForces

来源:互联网 发布:谷嫂淘宝 蓝盾 编辑:程序博客网 时间:2024/06/05 00:14

CodeForces - 893A Chess For Three

Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it’s a bit difficult for them because chess is a game for two players, not three.

So they play with each other according to following rules:

Alex and Bob play the first game, and Carl is spectating;
When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner.
Alex, Bob and Carl play in such a way that there are no draws.

Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it!

Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of games Alex, Bob and Carl played.

Then n lines follow, describing the game log. i-th line contains one integer ai (1 ≤ ai ≤ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game.

Output
Print YES if the situation described in the log was possible. Otherwise print NO.

Example
Input
3
1
1
2
Output
YES
Input
2
1
2
Output
NO
Note
In the first example the possible situation is:

Alex wins, Carl starts playing instead of Bob;
Alex wins, Bob replaces Carl;
Bob wins.
The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one.

题意:三个人下象棋,但是每次只能两个人对局,所以就规定:一开始1和2对局,3观战,谁输了之后下局换本局观战的人上场,赢的人继续对局。
现在给出n个数(只包含1/2/3),代表第i局赢的人的编号。
问这些编号是否都正确,如果不正确就输出NO,否则YES。

…代码写的有点冗杂,感觉能就是几行代码的题!却被我写了这么多,直接枚举就好了。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long int LL;int s[110];int main(){    int n;    while(cin>>n)    {        int a=1,b=2,i;        for(int i=1; i<=n; i++)            cin>>s[i];        for(i=1; i<=n; i++)        {            if(s[i]==a)            {                a=s[i];                if(s[i]==1)                {                    if(b==2)                        b=3;                    else                        b=2;                }                if(s[i]==2)                {                    if(b==1)                        b=3;                    else                        b=1;                }                if(s[i]==3)                {                    if(b==1)                        b=2;                    else                        b=1;                }            }            else if(s[i]==b)            {                b=s[i];                if(s[i]==1)                {                    if(a==2)                        a=3;                    else                        a=2;                }                if(s[i]==2)                {                    if(a==1)                        a=3;                    else                        a=1;                }                if(s[i]==3)                {                    if(a==1)                        a=2;                    else                        a=1;                }            }            else                break;        }        if(i==n+1)            cout<<"YES"<<endl;        else            cout<<"NO"<<endl;    }}

CodeForces - 893B Beautiful Divisors

Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautiful iff its binary representation consists of k + 1 consecutive ones, and then k consecutive zeroes.

Some examples of beautiful numbers:

12 (110);
1102 (610);
11110002 (12010);
1111100002 (49610).
More formally, the number is beautiful iff there exists some positive integer k such that the number is equal to (2k - 1) * (2k - 1).

Luba has got an integer number n, and she wants to find its greatest beautiful divisor. Help her to find it!

Input
The only line of input contains one number n (1 ≤ n ≤ 105) — the number Luba has got.

Output
Output one number — the greatest beautiful divisor of Luba’s number. It is obvious that the answer always exists.

Example
Input
3
Output
1
Input
992
Output
496

题意:新定义的 beautiful numbers:二进制表示为有k+1个连续的1,k个连续的0。现在给出一个数,求他的greatest beautiful divisor (emmm…最大的美丽因子!)

答案一定会存在,因为一定会有一个因子为1.

解题思路:想了一会,先判断这个数是否为素数,如果是素数直接输出1;

可以看出对于题目要求的beautiful numbers,符合条件的很少!于是就打了个表,果不其然,<=1e5的数字中符合条件的只有八个。打出表之后直接循环判断就可以了,注意从大到小判断,因为要输出最大的因子!

其实判断素数完全没必要,直接循环就好了,多此一举了。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;const int M=1e5;int check[M+50];string str;int ans[10]={1,6,28,120,496,2016,8128,32640,130816};void init(){    int i,j,len=1;    for(i=2; i*i<=M; i++)    {        if(!check[i])        {            for(j=i*i; j<M; j+=i)                check[j]=1;        }    }    check[1]=1;}int main(){    int n;    init();    while(~scanf("%d",&n))    {        if(!check[n])//素数直接输出1        {            cout<<1<<endl;            continue;        }        else        {            for(int i=8;i>=0;i--)            {                if(n%ans[i]==0)                {                    cout<<ans[i]<<endl;                    break;                }            }        }    }}

CodeForces 893C Rumor

Vova promised himself that he would never play computer games… But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.

Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.

Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.

The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?

Take a look at the notes if you think you haven’t understood the problem completely.

Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.

The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.

Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.

Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

Example
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.

In the second example Vova has to bribe everyone.

In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

题意:Vova要传播谣言,每向一个人传播谣言需要花费一定的费用,但是当把谣言传给别人的时候,别人可以把谣言免费传给自己的朋友,问:当所有人都知道谣言的情况下问最小花费

显然并查集,但是在合并的时候需要注意,把朋友的集合都并到花费最小的那个人身上,这样最后直接求和father数组里 father[i]=i的情况即可。

#define IO ios::sync_with_stdio(false);\cin.tie(0);\cout.tie(0);#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long int LL;const int MAXL(1e5);int father[MAXL+50];int haxi[MAXL+50];int Find(int x){    if(x!=father[x])        father[x]=Find(father[x]);    return father[x];}void Join(int x,int y){    int fx=Find(x),fy=Find(y);    if(fx!=fy)    {        if(haxi[fx]<haxi[fy])//分情况合并            father[fy]=fx;        else            father[fx]=father[fy];    }}int main(){    int n,m;    while(cin>>n>>m)    {        IO;        for(int i=0; i<=MAXL; i++)            father[i]=i;        for(int i=1; i<=n; i++)            cin>>haxi[i];        for(int i=1; i<=m; i++)        {            int x,y;            cin>>x>>y;            Join(x,y);        }        LL ans=0;        for(int i=1; i<=n; i++)        {            if(father[i]==i)                ans+=haxi[i];        }        cout<<ans<<endl;    }}
原创粉丝点击