【codeforces 709D】Recover the String
【题目链接】:http://codeforces.com/problemset/problem/709/D
【题意】
给你一个序列;
给出01子列和10子列和00子列以及11子列的个数;
然后让你输出一个符合要求的序列;
【题解】
这里
00和11可以确定出序列中0和1的个数;
但有边缘数据
00如果为0代表什么?
->没有0或者是有1个0
11如果为0代表什么?
->没有1或者是有1个1
对这两种情况需要特判一下(两种情况的特判需要用到01和10的数量)
看代码吧。
然后这两种情况排除之后;
00和11就都大于0了;
则可以根据
00=a0*(a0-1)/2
11=a1*(a1-1)/2
来求出0的个数和1的个数;
具体的;
你需要对2*00开根号->x
同时你还有算一下
(x+1)*x/2是不是等于00
因为如果不是的话就表示没有符合要求的0的个数的序列满足要求;
那种情况也要输出无解;
1同理;
然后我们算出1的个数和0的个数;
把所有的1都放在最前面,然后0的放在后面;
这样a10等于0的个数乘1的个数;
然后可以验证你每把最右边那个1往右交换一个0,a10会减1,a01会加1;
则按照这个规则;
每次看看a10和输入的a10的差为多少rest
while(rest>=num0){
**把最右边那个1和最右边那个0交换;
**rest-num;
}
然后rest可能还有剩余
则把最右边那个1和这个1往右数rest个0交换位置;
就能再减去rest个10了;
根据上述规则可知;
如果输入的a10+a01!=1的个数*0的个数
则输出无解;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
int a00, a01, a10, a11;
string s;
void work(int num0, int num1){
s = " ";
rep1(i, 1, num1) s += '1';
rep1(i, 1, num0) s += '0';
if (a01 + a10 != num0*num1) puts("Impossible"),exit(0);
int ta10 = num1*num0;
int j = num1,i = 0;
LL rest = ta10 - a10;
while (rest>=num0)
{
swap(s[j], s[num1 + num0 - i]);
j--; i++;
rest -= num0;
}
swap(s[j], s[j + rest]);
s.erase(0, 1);
cout << s << endl;
exit(0);
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(a00), rei(a01), rei(a10), rei(a11);
if (a00 == 0){
//no zero
if (a01 == 0 && a10 == 0){
int len = sqrt(2 * a11)+1;
if (len*(len - 1) == 2 * a11)
{
rep1(i, 1, len) printf("1");
return 0;
}
}
//one zero
if (a11 == 0){
//no one
if (a10 == 0 && a01 == 0) return puts("0"), 0;
//one one
if (a10 + a01 == 1){
if (a10 == 1) return puts("10"), 0;
if (a01 == 1) return puts("01"), 0;
}
}
else
if (a11 > 0) {
int len = sqrt(2 * a11) + 1;
if (len*(len-1)==2*a11)
work(1, int(sqrt(2 * a11) + 1));
}
puts("Impossible");
return 0;
}
if (a11 == 0)
{
//no one
if (a10 ==0 && a01 == 0) {
int len = sqrt(2 * a00) + 1;
if (len*(len - 1) == 2 * a00) {
rep1(i, 1, len) printf("0");
return 0;
}
}
//one one
assert(a00 > 0);
int len = sqrt(2 * a00) + 1;
if (len*(len - 1) == 2 * a00)
work(int(sqrt(2 * a00) + 1), 1);
puts("Impossible");
return 0;
}
int len = sqrt(2 * a00) + 1, len1 = sqrt(2 * a11) + 1;
if (len*(len-1)==2*a00 && len1*(len1-1)==2*a11)
work(int(sqrt(2 * a00) + 1), int(sqrt(2 * a11) + 1));
puts("Impossible");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 709D】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 ...
- 【Codeforces 1132F】Clear the String
Codeforces 1132 F 题意:给一个串\(S\),问每次删除连续的一段相同字母,最少删几次将原串删空. 思路:考虑区间\(dp\),我们看要删多少次能把\([l,r]\)删空,那么最终答案 ...
- 【CodeForces 624C】Graph and String
题 题意 n个表示abc三个字符的点,所有a和b是相连的,所有b和c是相连的,所有相同的是相连的,现在给你n个点和他们之间的m条边,判断是否存在这样的字符串,存在则给出一个符合条件的. 分析 我的做法 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【HDOJ 1009】 CRB and String
[HDOJ 1009] CRB and String 每组两个串s t 仅仅由小写字母组成 问从s能不能变成t 改变的操作为选一个字符 在后面加上一个与所选字符不同的字符 这样的操作能够做无数次 问能 ...
- 【codeforces 797C】Minimal string
[题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...
- 【codeforces 779D】String Game
[题目链接]:http://codeforces.com/contest/779/problem/D [题意] 给你一段操作序列; 按顺序依次删掉字符串1中相应位置的字符; 问你最多能按顺序删掉多少个 ...
- 【codeforces 801B】Valued Keys
[题目链接]:http://codeforces.com/contest/801/problem/B [题意] 定义一个对两个字符串x,y的f(x,y)函数; 返回的是一个字符串; 这个返回的字符串的 ...
- 【codeforces 801A】Vicious Keyboard
[题目链接]:http://codeforces.com/contest/801/problem/A [题意] 一个字符串只由VK组成; 让你修改一个字符; 使得剩下的字符串里面子串VK的个数最大; ...
随机推荐
- jQuery:has()和jQuery:contains()及jQuery:empty
jQuery:has()和jQuery:contains()两个方法比较类似.不同点在于: has是判断标签的 contains是判断文本的 1.jQuery:has() <div>< ...
- json返序列化
ASP.NET中JSON的序列化和反序列化 http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html 迟来的Json反序列化 ht ...
- 洛谷 P2365 任务安排【dp】
其实是可以斜率优化的但是没啥必要 设st为花费时间的前缀和,sf为Fi的前缀和,f[i]为分组到i的最小花费 然后枚举j转移,考虑每次转移都是把j到i分为一组这样意味着j及之后的都要增加s的时间,同时 ...
- bzoj 1828: [Usaco2010 Mar]balloc 农场分配【贪心+线段树】
长得挺唬人的贪心,按照右端点排序,用最小值线段树的询问判断当前牛是否能放进去,能的话更新线段树,ans++ 来自https://www.cnblogs.com/rausen/p/4529245.htm ...
- [Swift]圆周率π
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 51nod1459 迷宫游戏
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你 ...
- windows系统下的redis启动教程
下载解压后配置redis.conf文件配置端口号和密码,打开poweshell命令,进入redis解压目录,使用.\redis-server.exe redis.conf 命令启动redis服务,再打 ...
- SQlite数据库框架:LitePal
常用的数据库框架Android的发展的速度是难以置信的,Android出来哪一年我还在小学上学很,还能很清楚的记得,那年一切,但是那个时候的我怎么可能也不会想到自己将来会要去做Android.Andr ...
- 机器学习_K近邻Python代码详解
k近邻优点:精度高.对异常值不敏感.无数据输入假定:k近邻缺点:计算复杂度高.空间复杂度高 import numpy as npimport operatorfrom os import listdi ...
- C语言编辑编译及集成开发环境
C语言编辑编译及集成开发环境 编辑器 在不同的操作系统上使用不同的编辑器,保存源代码文件时,文件名应指出程序的功能扩展名应为.c. 编译器 编译器把源代码编译成机器语言的二进制指令即目标代码生成目标文 ...