AIM Tech Round 3 (Div. 2) D. Recover the String 构造、贪心、多坑、WA162
2016-08-26 23:59
453 查看
D. Recover the String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For each string s consisting of characters '0'
and '1' one can define four integers a00, a01, a10 and a11,
where axy is
the number ofsubsequences of length 2 of the string s equal
to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and
have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one
answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11.
Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length
of your answer must not exceed 1 000 000.
Examples
input
output
input
output
Source
AIM Tech Round 3 (Div. 2)
My Solution
构造、贪心、多坑、WA162
首先用杨辉三角求组合数打个表, 然后匹配一下找出 cnt11, cnt00,也就是1、0的个数 //这个做法比较暴力了嘿嘿 -_-||
然后如果 (a01 + a10 != cnt00*cnt11) 则没有答案
否则 初始是 000000111111这样的
然后每次if(a10 >= lef0){
cout<<1;
lef1--;
a10 -= lef0;
}
else{
cout<<0;
lef0--;
}
这样就是优先搞出大的10, 因为这个时候后面的0比较多 第一次 a10 >= lef0 就把 一个1提到最前面 a10 -= lef0, 否则是0 lef0--, 然后考虑下一位
此外就是 0 0 0 0, 0 1 0 0, 0 1 1 0, 1 0 0 0, 0 0 0 1这些数据的特殊处理了, 当统计出的cnt11 == -1 是 1的个数可能是1也可能是0 根据 a10 和 a01而定, cnt00 == -1同理
0 1 1 0 没有考虑, Wrong answer on test 162 呵呵哒 WA的最远的一次了 开心
复杂度O(n)
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
const LL Hash = 1e9 + 7;
inline LL mod(LL a)
{
return a - (a/Hash)*Hash;
}
LL C[maxn][6];
inline void getC()
{
memset(C, 0, sizeof C);
for(int i = 0; i < maxn; i++){
C[i][0] = 1;
for(int j = 1; j <= min(2, i); j++){
C[i][j] = mod(C[i-1][j-1] + C[i-1][j]);
}
}
}
string s;
int main()
{
#ifdef LOCAL
freopen("d.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
int T = 3;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
getC();
LL a00, a01, a10, a11, cnt00 = -1, cnt11 = -1, lef1 = 0, lef0 = 0;
cin>>a00>>a01>>a10>>a11;
for(int i = 2; i < maxn; i++){
if(a00 == C[i][2]){
cnt00 = i;
break;
}
}
for(int i = 2; i < maxn; i++){
if(a11 == C[i][2]){
cnt11 = i;
break;
}
}
if((a00 != 0 && cnt00 == -1) || (a11 != 0 && cnt11 == -1)) {cout<<"Impossible"<<endl;}
else{
//cout<<cnt00<<" "<<cnt11<<endl;
if(cnt00 == -1){
if(cnt11 == -1){ //WA17 0 0 1 0
if(a10 == 0 && a01 == 0) cout<<0<<endl;
else if(a10 == 0){
if(a01 == 1) cout<<"01"<<endl;
else cout<<"Impossible"<<endl;
}
else if(a01 == 0){
if(a10 == 1) cout<<"10"<<endl;
else cout<<"Impossible"<<endl;
}
else cout<<"Impossible"<<endl; //WA162 0 1 1 0
return 0;
}
if(a10 == 0 && a01 == 0){
for(int i = 0; i < cnt11; i++) cout<<"1";
cout<<endl;
return 0;
}
cnt00 = 1;
}
if(cnt11 == -1){
if(a10 == 0 && a01 == 0){
for(int i = 0; i < cnt00; i++) cout<<"0";
cout<<endl;
return 0;
}
cnt11 = 1;
}
/*
if(a10 > cnt00 * cnt11) cout<<"Impossible"<<endl;
else if(a01 > cnt00 * cnt11) cout<<"Impossible"<<endl;
*/
if(a10 + a01 != cnt00 * cnt11) cout<<"Impossible"<<endl;
else{
lef0 = cnt00; lef1 = cnt11;
for(int i = 0; i < cnt00 + cnt11; i++){
if(a10 >= lef0){
cout<<1;
lef1--;
a10 -= lef0;
}
else{
cout<<0;
lef0--;
}
}
cout<<endl;
}
}
#ifdef LOCAL
cout<<endl;
}
#endif // LOCAL
return 0;
}
Thank you!
------from ProLights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For each string s consisting of characters '0'
and '1' one can define four integers a00, a01, a10 and a11,
where axy is
the number ofsubsequences of length 2 of the string s equal
to the sequence {x, y}.
In these problem you are given four integers a00, a01, a10, a11 and
have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one
answer exists, there exists an answer of length no more than 1 000 000.
Input
The only line of the input contains four non-negative integers a00, a01, a10 and a11.
Each of them doesn't exceed 109.
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length
of your answer must not exceed 1 000 000.
Examples
input
1 2 3 4
output
Impossible
input
1 2 2 1
output
0110
Source
AIM Tech Round 3 (Div. 2)
My Solution
构造、贪心、多坑、WA162
首先用杨辉三角求组合数打个表, 然后匹配一下找出 cnt11, cnt00,也就是1、0的个数 //这个做法比较暴力了嘿嘿 -_-||
然后如果 (a01 + a10 != cnt00*cnt11) 则没有答案
否则 初始是 000000111111这样的
然后每次if(a10 >= lef0){
cout<<1;
lef1--;
a10 -= lef0;
}
else{
cout<<0;
lef0--;
}
这样就是优先搞出大的10, 因为这个时候后面的0比较多 第一次 a10 >= lef0 就把 一个1提到最前面 a10 -= lef0, 否则是0 lef0--, 然后考虑下一位
此外就是 0 0 0 0, 0 1 0 0, 0 1 1 0, 1 0 0 0, 0 0 0 1这些数据的特殊处理了, 当统计出的cnt11 == -1 是 1的个数可能是1也可能是0 根据 a10 和 a01而定, cnt00 == -1同理
0 1 1 0 没有考虑, Wrong answer on test 162 呵呵哒 WA的最远的一次了 开心
复杂度O(n)
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;
const LL Hash = 1e9 + 7;
inline LL mod(LL a)
{
return a - (a/Hash)*Hash;
}
LL C[maxn][6];
inline void getC()
{
memset(C, 0, sizeof C);
for(int i = 0; i < maxn; i++){
C[i][0] = 1;
for(int j = 1; j <= min(2, i); j++){
C[i][j] = mod(C[i-1][j-1] + C[i-1][j]);
}
}
}
string s;
int main()
{
#ifdef LOCAL
freopen("d.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
int T = 3;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
getC();
LL a00, a01, a10, a11, cnt00 = -1, cnt11 = -1, lef1 = 0, lef0 = 0;
cin>>a00>>a01>>a10>>a11;
for(int i = 2; i < maxn; i++){
if(a00 == C[i][2]){
cnt00 = i;
break;
}
}
for(int i = 2; i < maxn; i++){
if(a11 == C[i][2]){
cnt11 = i;
break;
}
}
if((a00 != 0 && cnt00 == -1) || (a11 != 0 && cnt11 == -1)) {cout<<"Impossible"<<endl;}
else{
//cout<<cnt00<<" "<<cnt11<<endl;
if(cnt00 == -1){
if(cnt11 == -1){ //WA17 0 0 1 0
if(a10 == 0 && a01 == 0) cout<<0<<endl;
else if(a10 == 0){
if(a01 == 1) cout<<"01"<<endl;
else cout<<"Impossible"<<endl;
}
else if(a01 == 0){
if(a10 == 1) cout<<"10"<<endl;
else cout<<"Impossible"<<endl;
}
else cout<<"Impossible"<<endl; //WA162 0 1 1 0
return 0;
}
if(a10 == 0 && a01 == 0){
for(int i = 0; i < cnt11; i++) cout<<"1";
cout<<endl;
return 0;
}
cnt00 = 1;
}
if(cnt11 == -1){
if(a10 == 0 && a01 == 0){
for(int i = 0; i < cnt00; i++) cout<<"0";
cout<<endl;
return 0;
}
cnt11 = 1;
}
/*
if(a10 > cnt00 * cnt11) cout<<"Impossible"<<endl;
else if(a01 > cnt00 * cnt11) cout<<"Impossible"<<endl;
*/
if(a10 + a01 != cnt00 * cnt11) cout<<"Impossible"<<endl;
else{
lef0 = cnt00; lef1 = cnt11;
for(int i = 0; i < cnt00 + cnt11; i++){
if(a10 >= lef0){
cout<<1;
lef1--;
a10 -= lef0;
}
else{
cout<<0;
lef0--;
}
}
cout<<endl;
}
}
#ifdef LOCAL
cout<<endl;
}
#endif // LOCAL
return 0;
}
Thank you!
------from ProLights
相关文章推荐
- 5个AI基础工具详解
- AI的标尺和混合工具学习教程
- Factorial Trailing Zeroes
- AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift 贪心、字典序
- AIM Tech Round 3 (Div. 2) B. Checkpoints 排序、讨论
- 看美剧《疑犯追踪》,学地道美语 Learn idiomatic American English by watching Tv series Person of Interest
- 《自动化焦虑》:《经济学人》关于自动化与人工智能的16篇专栏文章。4星
- No service of type Factory available in ProjectScopeServices
- No service of type Factory available in ProjectScopeServices
- 意识不仅存在人类---疯狂的想法
- Cannot nest 'PRJ/src/main/resource' inside 'PRJ/src'. To enable the nesting exclude 'main/' from 'PR
- Leetcode 11 Container With Most Water
- 揭开.NET消息循环的神秘面纱(GetMessage()无法取得任何消息,就会进入Idle(空闲)状态,进入睡眠状态(而不是Busy Waiting)。当消息队列不再为空的时候,程序会自动醒过来)
- 1014. Waiting in Line (30)
- HDU 2476 String painter (区间DP)
- windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)
- 无人驾驶的技术安全风险可以避免吗?
- In excel copy the cell that contain the special text to the new collom
- 一个AIDL的简单使用
- codeforces--707A. Brain's Photos