《1590: Herd Sums》

挺好的思维题吧,一开始觉得是规则题,但是没找出来。

后面想了下,尺取维护所有可能的区间即可,复杂度O(n)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e7+5;
const int M = 1e6+5;
const LL Mod = 1e9 + 7;
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}
        return x * f;
    }
}
using namespace FASTIO;

int a[N];
int main()
{
    int n;n = read();
    for(int i = 1;i <= n;++i) a[i] = i;
    int ans = 0,sum = 0,L = n,r = n;
    while(1)
    {
        if(sum == n)
        {
            ans++;
            sum -= a[r--];
        }
        else if(sum < n)
        {
            if(L == 0) break;
            sum += a[L--];
        }
        else sum -= a[r--];
    }
    printf("%d
",ans);
    system("pause");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/14073296.html