[NOIP2017模拟]太空电梯

来源:互联网 发布:linux samba 编辑:程序博客网 时间:2024/04/27 15:31

2017.8.29 T1 1952

样例数据
输入

6 6
1
2
3
3
4
5

输出

5

分析:基本上是用贪心,就是找正好一胖一瘦没法一起进电梯的一前一后,如果瘦的无法满足,就和另外一个瘦子一起把电梯人数挤满(注意最后只剩一个人时也要搭一次电梯)。

代码

#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();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());    if(ch=='-')    {        f=-1;        ch=getchar();    }    for(;ch>='0'&&ch<='9';ch=getchar())        sum=(sum<<3)+(sum<<1)+ch-48;    return sum*f;}int n,k,head,tail,ans,prehead=1;int v[100010];int main(){    freopen("elevator.in","r",stdin);    freopen("elevator.out","w",stdout);    n=getint();k=getint();    for(int i=1;i<=n;++i)        v[i]=getint();    sort(v+1,v+n+1);//排一下序    head=1,tail=n;//胖瘦组合    while(head<=tail)    {        ans++;//不管这两个是否能搭配上,无论如何也要至少乘一次电梯对吧        if(head==tail) break;//最后一个人的情况在这就停了,ans已经++了        if(v[head]+v[tail]>k)//能搭配上        {            tail--;//走一个胖子            ans++;//需要一次电梯        }        else            head++;//搭配不上,就再走一个瘦子,两个凑人数        head++;//瘦子无论如何都要走对吧    }    cout<<ans<<'\n';    return 0;}

本题结。