CSTC 2017 要匙

题链

 老了,这种题也调了一个小时。机房里一群人在搞基,无心写代码。

#include<bits/stdc++.h>
#define N 2000005
using namespace std;
int p[N],ppp[N],sum[N],*id=ppp+N/2,anw,now,px[N>>1];
int seed, n, k, S;
int getrand()
{
    seed = ((seed * 12321) ^ 9999) % 32768;
    return seed;
}
void generateData()
{
    scanf("%d%d%d",&k,&seed,&S);
    int t = 0;
    n = k * 2 + 1;
    memset(p, 0, sizeof(p));
    for (int i = 1; i <= n; i++)
    {
        p[i] = (getrand() / 128) % 2;
        t += p[i];
    }
    int i = 1;
    while (t > k)
    {
        while (p[i] == 0)
            i++;
        p[i] = 0;
        t--;
    }
    while (t < k)
    {
        while (p[i] == 1)
            i++;
        p[i] = 1;
        t++;
    }
}
int main () {
    freopen("a.in","r",stdin);
    generateData();
    for (int i=1;i<=n;i++) sum[i]=sum[i-1]+(p[i]?1:-1),
    id[sum[i]]+=1-p[i],anw+=(sum[i]>=0)&&(!p[i]);
    now=sum[n];
    for (int i=1;i<=n;i++) {
        if (p[i]) anw-=id[sum[i]-1]; else anw+=id[sum[i]];
        if (!p[i]) px[anw-1]=i;
        now=now+(p[i]?1:-1);
        if (!p[i])  {
         id[sum[i]]--;  anw--;
         id[now]++; if (now>=sum[i]) anw++; }
    }
    printf("%d\n",px[0]);
    printf("%d\n",px[S]);
    memset(sum,0,sizeof sum); memset(ppp,0,sizeof ppp); memset(px,0,sizeof px);
    now=anw=0;
    for (int i=1;i<=n;i++) sum[i]=sum[i-1]+(p[i]?-1:1),
    id[sum[i]]+=1-p[i],anw+=(sum[i]>0)&&(!p[i]);
    now=sum[n];
    for (int i=1;i<=n;i++) {
        if (!p[i]) anw-=id[sum[i]]; else anw+=id[sum[i]+1];
        if (!p[i]) px[anw]=i;
        now=now+(p[i]?-1:1);
        if (!p[i])  {
         id[sum[i]]--;  //anw--;
         id[now]++; if (now>sum[i]) anw++; }    
    }
    printf("%d\n",px[S]);
    return 0;
}

 

posted @ 2018-04-03 21:15  泪寒之雪  阅读(218)  评论(0编辑  收藏  举报