Recover the String
Recover the String
题目链接:http://codeforces.com/contest/709/problem/D
构造
这题乍一看很难构造,但是如果知道了整个字符串中'0'和'1'的个数n0和n1,就很好构造了:
1.将整个字符串填满'1',在所有的1右边填x个'0',那么'10'就有x*n1个了,一直填到还需要'10'的个数t1小于n1个;
2.再从右边数n1-t1个'1'并在它前面添加一个'0';
3.删去多余的'1'.
如此看来,构造并不困难,难的是判断何时Impossible.我们可以看到a1和a4必须是(1+n)*n/2的形式,而a2+a3肯定等于n0*n1(对于每一个'1',若左边有x个'0',那么右边有n0-x个'0')。但是这样直接交还会WA,还有一种情况没有考虑:只有'1'或只有'0'的情况,这种情况特判就好啦。
代码如下:
#include<cstdio>
#include<cmath>
using namespace std;
int a,b,c,d;
char s[];
int main(void){
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a+b+c+d==){
printf("1\n");
return ;
}
int n0=(int)sqrt(*a)+;
if(n0*(n0-)/!=a){
printf("Impossible\n");
return ;
}
int n1=(int)sqrt(*d)+;
if(n1*(n1-)/!=d){
printf("Impossible\n");
return ;
}
if(n0+n1>){
printf("Impossible\n");
return ;
}
if(c+b+d==){
for(int i=;i<n0;++i)
printf("");
printf("\n");
return ;
}else if(a+b+c==){
for(int i=;i<n1;++i)
printf("");
printf("\n");
return ;
}
if((c+b)!=n0*n1){
printf("Impossible\n");
return ;
}
for(int i=;i<n0+n1;++i)
s[i]='';
int t=c/n1;
for(int i=;i<t;i++)
s[n0+n1--i]='';
int t1=c-t*n1;
int k=,i;
for(i=n0+n1-;i>=;--i){
if(k==n1-t1){
s[i]='';
break;
}
if(s[i]=='')k++;
}
for(;i>=;--i){
if(s[i]=='')k++;
if(k>n1)s[i]='';
}
printf("%s\n",s);
}
Recover the String的更多相关文章
- codeforces 709D D. Recover the String(构造)
题目链接: D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input s ...
- AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)
D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...
- AIM Tech Round 3 (Div. 1) B. Recover the String 构造
B. Recover the String 题目连接: http://www.codeforces.com/contest/708/problem/B Description For each str ...
- 【CodeForces】708 B. Recover the String 数学构造
[题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...
- B. Recover the String
B. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...
- codeforces708b// Recover the String //AIM Tech Round 3 (Div. 1)
题意:有一个01组成的串,告知所有长度为2的子序列中,即00,01,10,11,的个数a,b,c,d.输出一种可能的串. 先求串中0,1的数目x,y. 首先,如果00的个数a不是0的话,设串中有x个0 ...
- CF708B Recover the String 构造
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 an ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- CodeForces 708B Recover the String
构造. 根据$a[0][0]$可以求得$0$的个数$p$,根据$a[1][1]$可以求得$1$的个数$q$. 如果找不到$p$或$q$,那么就无解. 每一个$0$放到序列中的任何一个位置,假设和前面的 ...
随机推荐
- C语言之冒泡排序
冒泡排序: 1). 简介 其实就是把一个数组的元素,按照从小到大(从大到小)得顺序,重新排列起来,这种排序就叫冒泡排序 例: int nums[5] = {5,4,3,2,1}; //经过排序后 下标 ...
- 通俗理解kalman filter原理
[哲学思想]即使我们对真相(真值)一无所知,我们任然可以通过研究事物规律,历史信息,当前观测而能尽可能靠近真相(真值). [线性预测模型]温度的变化是线性规律的,已知房间温度真值每小时上升1度左右(用 ...
- [ios2]判断retina 屏幕
判断是否retina屏幕 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSize ...
- java基础练习 11
import java.util.Scanner; public class Eleventh { /*给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. (5 分数)*/ p ...
- 0116MySql主从复制监控
如何自动化监控mysql主从服务-- 第一步创建目录mkdir -p /tmp/mysql/masterslave #日志存放目录在/root下面创建sh目录 #存放执行的SHELL脚本 -- 第二步 ...
- 四、spark常用函数说明学习
1.parallelize 并行集合,切片数.默认为这个程序所分配到的资源的cpu核的个数. 查看大小:rdd.partitions.size sc.paraliel ...
- [Q]关于不同比例、不同纸张大小图纸的批量打印
不同比例或不同纸张大小的图纸可以一起批量打印,但目前打图精灵不能自动识别纸张大小,纸张大小是手动指定的,且只能指定一种纸张大小,但设置打印比例为“缩放到可打印区域”或“缩放到纸张边界”仍然可以将不同比 ...
- Ubuntu中Qt新建窗体提示lGL错误
提示错误: cannot find -lGL collect2:error:ld returned 1 exit status 这是因为系统缺少链接库,终端输入: sudo apt-get insta ...
- QGIS
project(GisFreeMap) set(CMAKE_BUILD_TYPE Debug) find_package(Qt4 REQUIRED QtCore QtGui QtXml) includ ...
- python中的二维数组90度旋转
data=[[col for col in range(4)] for raw in range(4)] #创建一个二维数组 for n in data: print (n) print('\n') ...