Codeforces Round #224 (Div. 2)

来源:互联网 发布:ps网络培训班 编辑:程序博客网 时间:2024/05/22 07:43

A. Ksenia and Pan Scales
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium.

The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

Input

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "|" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.

The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.

It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

Output

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input.

If there are multiple answers, print any of them.

Sample test(s)
input
AC|TL
output
AC|TL
input
|ABCXYZ
output
XYZ|ABC
input
W|TF
output
Impossible
input
ABC|D
output
Impossible

题意:一个字母代表一个单位重的物体,给出一个表达式,其中 “|” 代表中心,然后给出一行字母代表物体,问是否可以使天平平衡

题解:简单的推理一下2边就行了,注意理解的是所有字母都是相同重量

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>char s1[100008];char s2[100008];int main(){    int i,j,left,right,len,res;    while(scanf("%s%s",s1,s2)>0)    {        for(left=i=0;s1[i]!='|';i++) left++;        for(right=0,i++;s1[i]!='\0';i++) right++;        len=strlen(s2);        if(len-abs(left-right)<0||(len-abs(left-right))&1) printf("Impossible\n");        else        {            res=(len-abs(right-left))/2;            if(left<right)            {                for(i=0;i<res;i++) printf("%c",s2[i]);                for(j=0;j<right-left;j++,i++) printf("%c",s2[i]);                printf("%s%s",s1,s2+i);                printf("\n");            }            else if(left==right)            {                for(i=0;i<res;i++) printf("%c",s2[i]);                printf("%s%s",s1,s2+i);                printf("\n");            }            else            {                for(i=0;i<res;i++) printf("%c",s2[i]);                printf("%s%s",s1,s2+i);                printf("\n");            }        }    }    return 0;}


C. Arithmetic Progression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills:

a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1.

For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.

Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards).

Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.

Output

If Arthur can write infinitely many distinct integers on the card, print on a single line -1.

Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).

Sample test(s)
input
34 1 7
output
2-2 10
input
110
output
-1
input
41 3 5 9
output
17
input
44 3 4 5
output
0
input
22 4
output
30 3 6

题解:用给出的n个数和一个你写的数组成一个等差数列,问你需要写的数是什么,有多少种情况,无数种情况输出-1

题解:模拟推理理解一下,不过分的情况有点多,容易漏情况,详细如代码

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;int a[100008];int main(){    int n,i,j,minx;    while(scanf("%d",&n)>0)    {        for(i=0;i<n;i++) scanf("%d",a+i);        sort(a,a+n);        if(n>1) minx=a[1]-a[0];        if(n==1)        {            printf("-1\n");            continue;        }        else if(a[0]==a[n-1])        {            printf("1\n");            printf("%d\n",a[0]);            continue;        }        else if(n==2)        {            if((a[1]-a[0])%2==1)            {                printf("2\n");                printf("%d ",a[0]-minx);                printf("%d\n",a[1]+minx);            }            else            {                printf("3\n");                printf("%d ",a[0]-minx);                printf("%d ",a[0]+minx/2);                printf("%d\n",a[1]+minx);            }            continue;        }        for(i=2;i<n;i++) if(a[i]-a[i-1]!=minx) break;        if(i<n)        {            for(j=i+1;j<n;j++) if(a[j]-a[j-1]!=minx) break;            if((a[i]-a[i-1])==minx*2&&j>=n)            {                printf("1\n");                printf("%d\n",a[i-1]+minx);            }            else            {                for(j=2;j<n;j++) if(2*(a[j]-a[j-1])!=minx) break;                if(j<n) printf("0\n");                else                {                    printf("1\n");                    printf("%d\n",a[0]+minx/2);                }            }        }        else        {            printf("2\n");            printf("%d ",a[0]-minx);            printf("%d\n",a[n-1]+minx);        }    }    return 0;}



0 0
原创粉丝点击