Educational Codeforces Round 137 (Rated for Div. 2) D. Problem with Random Tests

Educational Codeforces Round 137 (Rated for Div. 2) D. Problem with Random Tests

The first observation we need is that we can choose two prefixes of s s s as the substrings used in forming the results. This can be proved easily: suppose we chose a substring which does not contain the leftmost character of s s s; if we expand it to the left, the answer won’t become worse. So, it is optimal to choose two prefixes of s s s as the substrings.

Furthermore, one of these prefixes must be s s s itself: if the leftmost index of 1 is i i i, the length of the answer won’t exceed n − i + 1 n−i+1 ni+1, but the only way to have a 1 in the (n−i+1)-th bit of the answer is to choose a prefix of s s s where the ( n − i + 1 ) (n−i+1) (ni+1)-th character (from the right) is 1; and there is only one such prefix of s s s, which is s s s itself.

So, now we can solve the problem in O ( n 2 ) O(n^2) O(n2) — try to combine all prefixes of s s s with s s s itself, and choose the one that yields the best answer. To speed this up, we need to somehow cut down on the number of prefixes of s s s we check.

Let’s look at the first block of 1’s in s s s. The next character after this block is 0; since we take s s s as one of the substring, in order to get 1 instead of 0 in the corresponding position of the answer, we need to choose a prefix which has 1 in that position. This 1 represents one of the 1’s from the first block of 1’s, since only one of them can shift to that position. So, we need to check only the prefixes such that, by using them, we shift some character 1 from the first block to the position of the first 0 after this block. Since the tests are random, the expected length of the first block of 1’s is O ( 1 ) O(1) O(1) (furthermore, even the probabiliy that its length is 20 or bigger is about 1 0 − 6 10^{−6} 106), so the expected number of prefixes we need to check is also O ( 1 ) O(1) O(1). Thus, the expected runtime of our solution is O ( n ) O(n) O(n).

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;cin>>n;
    string s;cin>>s;

    if(string(n,'0')==s)
    {
        cout<<"0"<<endl;
        return 0;
    }

    int poss=0;
    while(s[poss]=='0') poss++;

    int pos=n;
    for(int i=poss;i<n;i++)
        if(s[i]=='0')
        {
            pos=i;
            break;
        }

    string mx=s;
    int len=n-pos;
    vector<string> b;
    for(int i=poss;i<n-len;i++)
        if(s[i]=='1')
            b.push_back(s.substr(i,len));

    for(auto ss:b)
    {
        string t=s;
        for(int i=pos,j=0;i<n;i++,j++)
            t[i]=(t[i]|ss[j]);
        mx=max(mx,t);
    }
    cout<<mx.substr(poss)<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值