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的更多相关文章

  1. codeforces 709D D. Recover the String(构造)

    题目链接: D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. 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 ...

  3. 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 ...

  4. 【CodeForces】708 B. Recover the String 数学构造

    [题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...

  5. B. Recover the String

    B. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. 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 ...

  7. CF708B Recover the String 构造

    For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 an ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. CodeForces 708B Recover the String

    构造. 根据$a[0][0]$可以求得$0$的个数$p$,根据$a[1][1]$可以求得$1$的个数$q$. 如果找不到$p$或$q$,那么就无解. 每一个$0$放到序列中的任何一个位置,假设和前面的 ...

随机推荐

  1. C语言之三目运算符

    三目运算符 三目运算符:也叫三元运算符.这个运算符的符号是: ? : 语法: 表达式1 ? 表达式2 : 表达式3; 语义: 先执行表达式1,执行完毕,表达式1的结果如果为真,那么执行表达式2,并且这 ...

  2. 微信企业号 JS-SDK:上传图片

    微信的JS-SDK提供了微信客户端相关的功能,如:拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享.扫一扫等微信特有的能力,为微信用户提供更优质的网页体验.这里将会介绍如何通过调用JS ...

  3. Mybatis学习笔记(四) 之动态SQL语句

    动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件 ...

  4. 使用Spring AOP来进行权限验证

    使用Spring AOP前需要先引入相应的包 <dependency> <groupId>org.aspectj</groupId> <artifactId& ...

  5. php学习笔记——文件(1)

    一.include和require 服务器端包含 (SSI) 用于创建可在多个页面重复使用的函数.页眉.页脚或元素. include (或 require)语句会获取指定文件中存在的所有文本/代码/标 ...

  6. tuple 可更改的列表和不可更改的元组

    tuple([iterable]) Return a tuple whose items are the same and in the same order as iterable's items. ...

  7. 双数组trie树的基本构造及简单优化

    一 基本构造 Trie树是搜索树的一种,来自英文单词"Retrieval"的简写,可以建立有效的数据检索组织结构,是中文匹配分词算法中词典的一种常见实现.它本质上是一个确定的有限状 ...

  8. mac git 的安装 及实现自动补全

    1.检查是否装了brew $ brew list如果没有,拷贝以下命令到终端 回车.可以安装好brewruby -e "$(curl -fsSL https://raw.githubuser ...

  9. (转载)js 快捷键大全,并有简单使用说明

    摘要: (转载)原文链接: http://www.cnblogs.com/fire-phoenix/archive/2010/09/28/1837295.html Code highlighting ...

  10. CODE[VS]-机票打折-浮点数处理-天梯青铜

    题目描述 Description 输入机票原价(3到4位的正整数,单位:元),再输入机票打折率(小数点后最多一位数字).编程计算打折后机票 的实际价格(单位:元.计算结果要将个位数四舍五入到十位数“元 ...