AtCoder Regular Contest 119 C - ARC Wrecker 2(同余定理+思维)
Problem Statement
There are buildings along the AtCoder Street, numbered through from west to east. Initially, Buildings have the heights of , respectively.
Takahashi, the president of ARC Wrecker, Inc., plans to choose integers and and make the heights of Buildings all zero. To do so, he can use the following two kinds of operations any number of times in any order:
- Set an integer and increase the heights of Buildings and by each.
- Set an integer and decrease the heights of Buildings and by each. This operation can only be done when both of those buildings have heights of or greater.
Note that the range of depends on .
How many choices of are there where Takahashi can realize his plan?
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
5 5 8 8 6 6
Sample Output 1
3
Takahashi can realize his plan for .
For example, for , the following sequence of operations make the heights of Buildings all zero.
- Decrease the heights of Buildings and by each, six times in a row.
- Decrease the heights of Buildings and by each, eight times in a row.
For the remaining seven choices of , there is no sequence of operations that can realize his plan.
Sample Input 2
7 12 8 11 3 3 13 2
Sample Output 2
3
Takahashi can realize his plan for .
For example, for , the following figure shows one possible solution.

Sample Input 3
10 8 6 3 9 5 4 7 2 1 10
Sample Output 3
1
Takahashi can realize his plan for only.
Sample Input 4
14 630551244 683685976 249199599 863395255 667330388 617766025 564631293 614195656 944865979 277535591 390222868 527065404 136842536 971731491
Sample Output 4
8
题意:
有 n 个数,每次可以将 a[x],a[x+1] 同时 +1/-1 ,问存在多少区间同时减为 0
题解:
直觉告诉我用差分来做,然后模拟出来的结果时间复杂度都是 O(n^2)
直到看到了 hu_tao 大佬的讨论,一语惊醒,每次操作一定是将一个奇数位和偶数位同时操作,所以无论怎么变一个区间想要同时变为 0,那么这个区间上奇数位置和偶数位置的加和是相同的;
所以将奇数位置处的值置为负数,利用同余定理,就可以找到任意一个连续的子段加和为 0
const int N=1e6+5;
int n, m, _;
int i, j, k;
ll a[N];
map<ll,int> mp;
signed main()
{
//IOS;
while(~sd(n)){
for(int i=1;i<=n;i++){
sll(a[i]);
if(i%2==0) a[i]=-a[i];
}
ll ans=0;
mp[0]++;
for(int i=1;i<=n;i++){
a[i]+=a[i-1];
if(mp[a[i]]) ans+=mp[a[i]];
mp[a[i]]++;
}
pll(ans);
mp.clear();
}
//PAUSE;
return 0;
}

浙公网安备 33010602011771号