Codeforces Round #224 (Div. 2)
来源:互联网 发布:ps网络培训班 编辑:程序博客网 时间:2024/05/22 07:43
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.
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.
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.
AC|TL
AC|TL
|ABCXYZ
XYZ|ABC
W|TF
Impossible
ABC|D
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;}
题解:模拟推理理解一下,不过分的情况有点多,容易漏情况,详细如代码
#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;}
- Codeforces Round #224 (Div. 2)
- Codeforces Round #224 (Div. 2)
- Codeforces Round #224 (Div. 2)
- Codeforces Round #224 (Div. 2)
- Codeforces Round #224 (Div. 2)
- Codeforces Round #224 (Div. 2)(数学、dfs)
- Codeforces Round #224 (Div. 2)解题报告
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- Codeforces Round #110 (Div. 2)
- Codeforces Round #122 (Div. 2)
- Codeforces Round #121 (Div. 2)
- FPGA串口通讯时波特率不稳定导致接受异常
- IE打开网页白屏
- 4G手机新玩法 全面互联网化
- Java兔子问题
- poj 3273 Tree(树链剖分)
- Codeforces Round #224 (Div. 2)
- ghjk
- 页面高度获取
- Ua10003 Cutting Sticks DP经典题目
- ios开发之MPNowPlayingInfoCenter 锁屏显示正在播放的音乐
- Linux makefile 教程 非常详细,且易懂
- CentOS 6.4 配置 KVM虚拟机笔记(快速简单方便安装)
- 类的多态的实现
- 二叉树、红黑树