01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)

题意:https://codeforc.es/problemset/problem/1204/D2

给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 l~r 的LIS最长上升子序列长度不变)。

问:0111001100111011101000

答:0011001100001011101000

思路:

参考题解:考虑什么串无法该变。

1. 10无法被改变(我们称为固定串),00,01,11都可以改。

2. 固定串+固定串(无法被改变)。

3. 1+固定串+0(无法被改变)。

所以固定串也就是类似于:111000101100 之类的。

容易想到:固定串是可以直接删除的,比如:01+110010+1 == 00+110010+0 ,不会影响前后(而且我们发现0和1的数量相等),而且你也不可能去改固定串。

所以我们可以不断的剔除固定串。

那剩下的1就全是可以改为0的了。

问题也就转变成了一个类似括号匹配的问题,我们就从后往前记录0的个数,把能匹配10的1标记好,最后把没有标记的1变成0就行了。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 void swapp(int &a,int &b);
 32 double fabss(double a);
 33 int maxx(int a,int b);
 34 int minn(int a,int b);
 35 int Del_bit_1(int n);
 36 int lowbit(int n);
 37 int abss(int a);
 38 //const long long INF=(1LL<<60);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 const int inf=(1<<30);
 42 const double ESP=1e-9;
 43 const int mod=(int)1e9+7;
 44 const int N=(int)1e6+10;
 45 
 46 char s[N];
 47 bool f[N];
 48 
 49 int main()
 50 {
 51     int l;
 52     s[0]='$';
 53     sc("%s",s+1);
 54     l=strlen(s)-1;
 55     int cnt=0;
 56     for(int i=l;i>=1;--i)
 57     {
 58         if(s[i]=='0')
 59             cnt++;
 60         else
 61         {
 62             if(cnt)
 63                 cnt--,f[i]=1;
 64         }
 65     }
 66     fo(i,1,l)
 67         if(s[i]=='1'&&!f[i])
 68             s[i]='0';
 69     pr("%s\n",s+1);
 70     return 0;
 71 }
 72 
 73 /**************************************************************************************/
 74 
 75 int maxx(int a,int b)
 76 {
 77     return a>b?a:b;
 78 }
 79 
 80 void swapp(int &a,int &b)
 81 {
 82     a^=b^=a^=b;
 83 }
 84 
 85 int lowbit(int n)
 86 {
 87     return n&(-n);
 88 }
 89 
 90 int Del_bit_1(int n)
 91 {
 92     return n&(n-1);
 93 }
 94 
 95 int abss(int a)
 96 {
 97     return a>0?a:-a;
 98 }
 99 
100 double fabss(double a)
101 {
102     return a>0?a:-a;
103 }
104 
105 int minn(int a,int b)
106 {
107     return a<b?a:b;
108 }

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/11447119.html
今日推荐