Codeforces Round #242 (Div. 2) (A、B、C)

来源:互联网 发布:java实现支付功能 编辑:程序博客网 时间:2024/04/27 18:27

A:

A. Squats
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.

For another exercise, Pasha needs exactly  hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?

Input

The first line contains integer n (2 ≤ n ≤ 200n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.

Output

In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.

Sample test(s)
input
4xxXx
output
1XxXx
input
2XX
output
1xX
input
6xXXxXx
output
0xXXxXx

题意:

       有n个仓鼠站成一列,每只仓鼠要么站着,要么坐着。每分钟可以让某只仓鼠改变状态,问最少需要多少分钟,是的站着的仓鼠和坐着的仓鼠各一半。


分析:

被坑了,把“some hamster”看成了“some hamsters”,以为只要一次可以随便改几个-_-|||

简单的统计和修改


代码:

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int n;char str[234];int main(){    while(~scanf("%d",&n)){        scanf("%s",str);        int stand = 0,sit = 0;        int len = strlen(str);        for(int i=0;i<len;i++){            if(str[i] == 'X'){                stand ++;            }            else{                sit ++;            }        }        if(sit == stand){            puts("0");            puts(str);        }        else{            if(stand>sit){                printf("%d\n",stand - (n/2));                for(int i=0;stand != sit;i++){                    if(str[i] == 'X'){                        str[i] = 'x';                        stand--;                        sit++;                    }                }                puts(str);            }            else{                printf("%d\n",sit - (n/2));                for(int i=0;stand != sit;i++){                    if(str[i] == 'x'){                        str[i] = 'X';                        stand++;                        sit--;                    }                }                puts(str);            }        }    }    return 0;}

B:

B. Megacity
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city.

The city of Tomsk can be represented as point on the plane with coordinates (00). The city is surrounded with n other locations, the i-th one has coordinates (xiyi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city.

Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity.

Input

The first line of the input contains two integers n and s (1 ≤ n ≤ 1031 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value.

It is guaranteed that no two locations are at the same point and no location is at point (0; 0).

Output

In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity.

The answer is considered correct if the absolute or relative error don't exceed 10 - 6.

Sample test(s)
input
4 9999981 1 12 2 13 3 12 -2 1
output
2.8284271
input
4 9999981 1 22 2 13 3 12 -2 1
output
1.4142136
input
2 11 1 9999972 2 1
output
-1

题意:

       在原点上有一个城市,人口为s(s<10^6),有n个小城市的坐标和人口数,原点上的城市想要扩大城市范围来增加人口,使其变为人口不小于10^6的特大城市,问所需最小的扩大半径是多少。


分析:

       算出每个点到原点的距离,然后按照距离排序,遍历一次即可算出最小的半径。


代码:

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;struct POINT{    int x,y;    int people;    int dis;}p[1234];int n,s;//int Dist(POINT a,POINT b){//    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));//}bool cmp(POINT a,POINT b){//    if(a.dis == b.dis){//        return a.people>b.people;//    }    return a.dis<b.dis;}int main(){    while(~scanf("%d%d",&n,&s)){        for(int i=0;i<n;i++){            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].people);            p[i].dis = p[i].x*p[i].x+p[i].y*p[i].y;        }        sort(p,p+n,cmp);//        cout<<"**********"<<endl;//        for(int i=0;i<n;i++){//            printf("%d  %d  %d  %d\n",p[i].x,p[i].y,p[i].people,p[i].dis);//        }//        cout<<"**********"<<endl;        int r = 0;        bool flag = false;        for(int i=0;i<n;i++){            s += p[i].people;            if(s >= 1000000){                flag = true;                r = p[i].dis;                break;            }        }        if(flag){            printf("%.7f\n",sqrt(r*1.0000000));        }        else{            puts("-1");        }    }    return 0;}

C:

C. Magic Formulas
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

People in the Tomskaya region like magic formulas very much. You can see some of them below.

Imagine you are given a sequence of positive integer numbers p1p2, ..., pn. Lets write down some magic formulas:

Here, "mod" means the operation of taking the residue after dividing.

The expression  means applying the bitwise xor (excluding "OR") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor".

People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q.

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 106). The next line contains n integers:p1, p2, ..., pn (0 ≤ pi ≤ 2·109).

Output

The only line of output should contain a single integer — the value of Q.

Sample test(s)
input
31 2 3
output
3

分析:

可以先写出一张表找找规律:

i: 1    2    3    4    5    6    7   8  .....

    ______________________________

1 |  0    1    1    1    1    1    1    1 .....

2 |  0    0    2    2    2    2    2    2 .....

|  0    1    0    3    3    3    3    3 .....

|  0    0    1    0    4    4    4    4 .....

5 |  0    1    2    1    0    5    5    5 .....

|  0    0    0    2    1    0    6    6 .....

|  0    1    1    3    2    1    0    7 .....

|  0    0    2    0    3    2    1    0 .....

.  |   .    .    .     .    .     .     .     .

.  |   .    .     .     .     .     .     .     .

       因为采用的mod运算,所以每一列都是一段一段的,每一段都是由1~i-1和0组成,所以可以将1~n连续的XOR值存起来,然后算出每一列的段数和剩余的数,如果段数为偶数,根据XOR的性质“相同为0,不同为1”,偶数段就可以忽略;如果段数为奇数,那么除去偶数段剩下的那一段的XOR值就可以直接从刚刚存起来的数组里面取。然后再处理剩余的数。


代码:

#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#define MAXN 1234567using namespace std;int n;int f[MAXN];int main(){    while(~scanf("%d",&n)){        int ans,tmp;        scanf("%d",&tmp);        ans = tmp;        for(int i=2;i<=n;i++){            scanf("%d",&tmp);            ans^=tmp;        }        f[0] = 0;        for(int i=1;i<=n;i++){            f[i] = f[i-1]^i;        }        for(int i=2;i<=n;i++){            int seg = n/i;            if(seg & 1 == 1) ans ^= f[i-1];            ans ^= f[n%i];        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击