[NOIP2017模拟]序列操作

2017.11.6 T2 2038

样例数据1
输入

3 5
1 2 5
1 2 3 2 1

输出

3

样例数据2
输入

5 5
1 3 3 4 5
1 2 4 4 4

输出

5

分析:看到都知道应该贪心每次选最大的去-1,就是看实现了(O( N2 )的随随便便都应该能先想到保底对吧)。对于考场上就是暴力还写炸的我,还是讲讲正解吧……
1、用桶乱搞,开一个桶数组记录每一种数值有多少个,每次就从最大的开始找到c个并把这些被减数值的个数移到数值-1的地方去,由于数值一共就10000种,所以每组数据c再大也没法让我移动超过10000次,再加上本身n就没c大,也限制了c,胡乱想想发现确实不好卡这种算法。
2、二分+贪心,二分能分的次数,记录每个数需要被减几次(用贪心,每次都从最大的开始,c=1的时候就是最大数-1,c=2的时候就是最大数和次大数-1,反正尽量让大数去被减,某个数的数值不够减就往后移),最后比较够不够减,所以就根据能否实现二分查找(貌似用语言说不清楚orz,看代码吧,虽然代码也不清楚orzorz)。
3、线段树,还是很暴力的实现了,感觉和乱搞差不多,只是有一些标记优化。

代码
乱搞玄学

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;

int getint()
{
    int sum=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
    if(ch=='-')
    {
        f=-1;
        ch=getchar();
    }
    for(;isdigit(ch);ch=getchar())
        sum=(sum<<3)+(sum<<1)+ch-48;
    return sum*f;
}

const int maxn=1000010;
int n,m,c,maxh,tot;
int h[maxn],cnt[10010];
bool bj;

int main()
{
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);

    n=getint(),m=getint();
    for(int i=1;i<=n;++i)
    {
        h[i]=getint();
        if(h[i]>0)
        {
            cnt[h[i]]++;
            maxh=max(maxh,h[i]);
        }
    }

    for(int i=1;i<=m;++i)
    {
        c=getint();
        tot=0,bj=false;
        for(int j=maxh;j>=1;--j)
        {
            tot+=cnt[j];
            if(tot>=c)
            {
                bj=true;
                cnt[j-1]+=cnt[j]-(tot-c);
                cnt[j]=tot-c+cnt[j+1];
                for(int k=j+1;k<=maxh;++k)
                    cnt[k]=cnt[k+1];
                if(j!=maxh||(tot==c&&j==maxh))
                {
                    cnt[maxh]=0;
                    maxh=maxh-1;
                }
                break;
            }
        }

        if(bj==false)
        {
            cout<<i-1<<'\n';
            return 0;
        }
    }

    cout<<m<<'\n';
    return 0;
}

二分标算,真的说不清楚,意会!意会!

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;

int getint()
{
    int sum=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
    if(ch=='-')
    {
        f=-1;
        ch=getchar();
    }
    for(;isdigit(ch);ch=getchar())
        sum=(sum<<3)+(sum<<1)+ch-48;
    return sum*f;
}

const int maxn=1000010;
int h[maxn],c[maxn];
int n,m;
int cnt[maxn];

bool comp(const int &a,const int &b)
{
    return a>b;
}

bool check(int k)
{
    for(int i=0;i<=n+1;++i)
        cnt[i]=0;

    long long sum=0;
    for(int i=1;i<=k;++i)
    {
        if(c[i]>n)//如果有一次操作需要-1的个数比n还大,显然就不行
            return false;
        cnt[c[i]]++;//记录每种c值的数量(方便记录前缀和)
        sum=sum+c[i];//记录需要减去的总和
    }

    for(int i=n;i>=1;--i)
        cnt[i]+=cnt[i+1];//记录前缀和(相当于计算每个数需要被减几次,比如最大数就需要减cnt[1]次,体现贪心的思想,让尽量多的-1由大数承担,也就只有大数能-1更多次嘛)

    long long val=0;
    for(int i=1;i<=n;++i)
    {
        val=val+min(h[i],cnt[i]);//记录能够减的数值的总和
        if(h[i]<cnt[i])
            cnt[i+1]+=cnt[i]-h[i];//不够就要把剩下的移到下一个数需要减的次数里
    }
    return val==sum;//最终判断移动完到底能不能减去sum个1
}

int main()
{
    //freopen("sequence.in","r",stdin);
    //freopen("sequence.out","w",stdout);

    n=getint(),m=getint();
    for(int i=1;i<=n;++i)
        h[i]=getint();

    sort(h+1,h+n+1,comp);

    for(int i=1;i<=m;++i)
        c[i]=getint();

    int l=0,r=m,ans=0;
    while(l<=r)//二分能够进行多少次操作
    {
        int mid=l+r>>1;
        if(check(mid))
            ans=mid,l=mid+1;
        else
            r=mid-1;
    }

    cout<<ans<<'\n';
    return 0;
}

本题结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值