【11.10】P76 T1

来源:互联网 发布:闲鱼淘宝介入后卖家胜 编辑:程序博客网 时间:2024/06/09 01:02

【 问题描述】
一张长度为n的纸带, 我们可以从左至右编号为0 − n( 纸带最左端标号为0)。 现在有m次操作, 每次将纸带沿着某个位置进行折叠, 问所有操作之后纸带的长度是多少。

【输入格式】
第一行两个数字n, m如题意所述。
接下来一行n个整数代表每次折叠的位置。

【输出格式】
一行一个整数代表答案。

【样例输入】
5 2
3 5

【样例输出】
2

【样例解释】
树上有只鸟。

【数据规模与约定】
对于60%的数据, n, m ≤ 3000。
对于100%的数据, n≤ 1018, m ≤ 3000。

模拟
对于一条纸带,在x位置折叠,长度会变成max(x - l,r - x);
维护叠过来的部分
2 * l - x; 2 * r - x;

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define ull unsigned long longconst int MAXN = 3005;ull n,m,x,num[MAXN];ull l,r,ans = 0;inline ull read(){    ull x = 0,f = 1;    char ch = getchar();    while(ch > '9'|| ch < '0')    {        if(ch == '-')  f = -1;        ch = getchar();    }    while(ch <= '9'&& ch >= '0')    {        x = x * 10 + ch - '0';        ch = getchar();    }    return x * f;}int main(){    freopen("he.in","r",stdin);    freopen("he.out","w",stdout);    memset(num,0,sizeof(num));    n = read();m = read();    l = 0,r = n;//zuo you    for(int i = 1; i <= m; i ++)          num[i] = read();     for(int i = 1; i <= m; i ++)    {        //changdu        if(num[i] * 2 >= l + r)     r = num[i]; //xuan zuo bian        else l = num[i];// xuan you bian        for(int j = i + 1; j <= m; j ++)        {            if(num[j] > r)  num[j] = r * 2 - num[j];            if(num[j] < l)  num[j] = l * 2 - num[j];        }    }    ans = r - l;    cout << ans << endl;    fclose(stdin);    fclose(stdout);    return 0;}
0 0
原创粉丝点击