#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> values;
struct SegTree{
int size;
vector<int> nodes;
void setup(int _size){
size = _size;
nodes.assign(size * 2, 0);
}
int getVal(int pos){
pos += size;
int out = nodes[pos];
pos /= 2;
while(pos){
out += nodes[pos];
pos /= 2;
}
return out;
}
void updateBetween(int from, int to, int val){
from += size;
to += size;
while(from <= to){
int pos = from, pot = 1;
while(from + pot * 2 <= to){
if(pos % 2) break;
pos /= 2;
pot *= 2;
}
nodes[pos] += val;
from += pot;
}
}
};
SegTree st;
bool check(int v){
queue<pair<int, int>> removals;
int tsum = 0;
vector<int> tmp(values);
for(int i = 0; i<values.size(); i++){
while(removals.size() and i - removals.front().second >= v){
tsum -= removals.front().first;
removals.pop();
}
tmp[i] -= tsum;
if(tmp[i] < 0) return false;
int cval = tmp[i];
if(cval == 0) continue;
tsum += cval;
removals.push({cval, i});
}
while(removals.size() and (int)values.size() - (int)removals.front().second >= v){
tsum -= removals.front().first;
removals.pop();
}
if(removals.size()) return false;
return true;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n; cin>>n;
values.resize(n);
int sum = 0, mval = 0;
for(int i = 0; i<n; i++){
cin>>values[i];
sum += values[i];
mval = max(mval, values[i]);
}
int out = 1;
for(int i = 2; i <= n; i++){
if(i * mval > sum) break;
if(sum % i) continue;
if(check(i)) out = i;
}
cout<<out<<endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <bits/stdc++.h> using namespace std; #define int long long vector<int> values; struct SegTree{ int size; vector<int> nodes; void setup(int _size){ size = _size; nodes.assign(size * 2, 0); } int getVal(int pos){ pos += size; int out = nodes[pos]; pos /= 2; while(pos){ out += nodes[pos]; pos /= 2; } return out; } void updateBetween(int from, int to, int val){ from += size; to += size; while(from <= to){ int pos = from, pot = 1; while(from + pot * 2 <= to){ if(pos % 2) break; pos /= 2; pot *= 2; } nodes[pos] += val; from += pot; } } }; SegTree st; bool check(int v){ queue<pair<int, int>> removals; int tsum = 0; vector<int> tmp(values); for(int i = 0; i<values.size(); i++){ while(removals.size() and i - removals.front().second >= v){ tsum -= removals.front().first; removals.pop(); } tmp[i] -= tsum; if(tmp[i] < 0) return false; int cval = tmp[i]; if(cval == 0) continue; tsum += cval; removals.push({cval, i}); } while(removals.size() and (int)values.size() - (int)removals.front().second >= v){ tsum -= removals.front().first; removals.pop(); } if(removals.size()) return false; return true; } signed main(){ cin.tie(0)->sync_with_stdio(0); int n; cin>>n; values.resize(n); int sum = 0, mval = 0; for(int i = 0; i<n; i++){ cin>>values[i]; sum += values[i]; mval = max(mval, values[i]); } int out = 1; for(int i = 2; i <= n; i++){ if(i * mval > sum) break; if(sum % i) continue; if(check(i)) out = i; } cout<<out<<endl; return 0; } |
English