1. 程式人生 > >codeforces Recover the String

codeforces Recover the String

題目很好推 00的個數推0的個數 11的個數推1的個數 如果存在那麼就判斷四個數的合是否為 C(0和1的總個數,2)如果存在有解貪心一下就行即根據第二個數值和1的位置固定然後叉進0最後使其成立
有個超級坑的就是00或11的個數 是0時那0或1的個數是0 或者是1要特殊判斷!!!!

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define maxn 1000005
#define LL long long
using namespace std;
LL am[maxn];
LL a, b, c, d;
LL bin(LL a)
{
   LL l = 1
, r = 1000001, ans = -1; for(LL i = 0; i <= 1000001; i++) if(i * (i - 1) == 2 * a) return i; return ans; } int main() { scanf("%I64d %I64d %I64d %I64d", &a, &b, &c, &d); LL am1 = bin(a), am2 = bin(d); //特判程式碼 if(am1 == 0 && am2 == 0) { if(2 * (a + b + c + d) == (am1 + 1
+ am2) * (am1 + am2))am1++; else if(2 * (a + b + c + d) == (am1 + 2 + am2) * (am1 + 1 + am2)){ am1++; am2++;} } else if(am1 == 0) {if(2 * (a + b + c + d ) == (am1 + 1 + am2) * (am1 + am2))am1++;} else if(am2 == 0) { if(2 * (a + b + c + d) == (am1 + 1 + am2) * (am1 + am2))am2++; } //-----------------------------------------------------------------
if(am1 == -1 || am2 == -1) printf("Impossible\n"); else if(a + b + c + d != (am1 + am2) * (am1 + am2 - 1) / 2) printf("Impossible\n"); else if(a + b + c + d == 0)printf("1\n"); else { LL pre = b; for(int i = 1; i<= am1; i++) { LL pre2 = min(am2, pre); am[am2 - pre2 + 1]++; pre -= pre2; } for(int i = 1; i <= am2; i++) { for(int j = 0; j < am[i]; j++)printf("0"); printf("1"); } for(int j = 0; j < am[am2 + 1]; j++)printf("0"); } return 0; }