NOIP 2015 Senior 4

来源:互联网 发布:北京数据恢复培训 编辑:程序博客网 时间:2024/05/21 05:43

我就不说是题什么了,看了标题应该都懂。这道题就是一个二分答案,并写check函数。check时要用到贪心的思想:从前往后不用搬的石头就留着,能搬的石头就搬。但是这道题需要把最后一个石头也加进来进行相同的操作。可以证明,当得到答案时,最后一个石头一定没有被搬?不会证明?好吧,其实我也不会。因为这道题做过的,所以我就这么做了,但是正确性可能并不是很好证明。这时一定要多出数据,确保不要失误。
听说大犇都是10分钟写完程序,花50分钟用暴力程序对拍。如果有时间我也要学会这么做。。。

参考代码

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <stack>#include <queue>#include <deque>#include <map>#include <set>using std::cin;using std::cout;using std::endl;inline int readIn(){    int a;    scanf("%d",&a);    return a;}const int maxn = 50005;int l, n, m;int river[maxn];bool check(int interval){    int count_ = 0;    int lastPos = 0;    for(int i = 0; i <= n; i++)    {        if(river[i] - lastPos >= interval)        {            lastPos = river[i];        }        else        {            count_++;        }    }    return count_ <= m;}void run(){    l = readIn();    n = readIn();    m = readIn();    for(int i = 0; i < n; i++)    {        river[i] = readIn();    }    river[n] = l;    int left = 0, right = l;    while(right - left > 1)    {        int mid = left + (right - left) / 2;        if(check(mid))        {            left = mid;        }        else        {            right = mid;        }    }    if(check(right)) left++;    printf("%d\n", left);}int main(){    run();    return 0;}
原创粉丝点击