/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>
#include <random>
#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
using namespace std;
random_device dev;
mt19937 rng(dev());
LL P[110000];
LL C[110000];
bool T[110000];
LL n;
void readData()
{
cin >> n;
FOR(i,1,n) cin >> P[i];
}
bool check(LL k)
{
if (k==1) return true;
FOR(i,1,n) C[i]=0;
LL open=0; LL waves=0;
LL endIndex=k;
FOR(i,1,n-k+1)
{
if (P[i]<open) return false;
else if (P[i]>open)
{
waves=P[i]-open;
open+=waves;
C[endIndex]=waves;
}
endIndex++;
open-=C[i];
}
FOR(i,n-k+2,n)
{
if (open!=P[i]) return false;
open-=C[i];
}
return true;
}
LL simpleAnswer()
{
FORD(k,n,2) if (check(k)) return k;
return 1;
}
void eliminate(LL k)
{
LL r;
if (!check(k))
{
r=k;
while (r<=n) { T[r]=false; r+=k; }
}
}
LL answer()
{
if (check(n)) return n;
FOR(i,1,n) T[i]=true;
FOR(i,2,MIN(n/2,8000)) if (T[i]) eliminate(i);
/*
eliminate(2);
eliminate(3);
if (T[4]) eliminate(4);
eliminate(5);
eliminate(7);
if (T[8]) eliminate(8);
if (T[9]) eliminate(9);
eliminate(11);
eliminate(13);
if (T[16]) eliminate(16);
eliminate(17);
*/
FORD(k,n,2) if (T[k]) { if (check(k)) return k; }
return 1;
}
void hacePlaya(LL k)
{
uniform_int_distribution<mt19937::result_type> distrA(0,5);
FOR(i,1,n) P[i]=0;
FOR(i,1,k) P[i]=1;
FOR(i,1,n-k+1)
{
LL waves=distrA(rng);
if (waves>0)
{
FOR(j,i,i+k-1) P[j]+=waves;
}
}
}
/// MAIN
int main(int argc, char* argv[])
{
// magic formula, which makes streams work faster:
ios_base::sync_with_stdio(0);
/*
n=8;
uniform_int_distribution<mt19937::result_type> distrA(3,n-1);
set<int> s;
while(true)
{
LL k=distrA(rng);
while(k%2==0) k=distrA(rng);
hacePlaya(k);
//FOR(i,1,n) cout << P[i] << " "; cout << "\n"; cout.flush();
if (check(2))
{
LL ans = answer();
if (ans==n-1)
{
FOR(i,1,n) cout << P[i] << " "; cout << "\n"; cout.flush();
}
if (s.count(ans)==0)
{
s.insert(ans);
if (ans%2==1) cout << "!!! ";
cout << ans << "\n"; cout.flush();
}
}
}
*/
readData();
cout << answer();
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #include <random> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; random_device dev; mt19937 rng(dev()); LL P[110000]; LL C[110000]; bool T[110000]; LL n; void readData() { cin >> n; FOR(i,1,n) cin >> P[i]; } bool check(LL k) { if (k==1) return true; FOR(i,1,n) C[i]=0; LL open=0; LL waves=0; LL endIndex=k; FOR(i,1,n-k+1) { if (P[i]<open) return false; else if (P[i]>open) { waves=P[i]-open; open+=waves; C[endIndex]=waves; } endIndex++; open-=C[i]; } FOR(i,n-k+2,n) { if (open!=P[i]) return false; open-=C[i]; } return true; } LL simpleAnswer() { FORD(k,n,2) if (check(k)) return k; return 1; } void eliminate(LL k) { LL r; if (!check(k)) { r=k; while (r<=n) { T[r]=false; r+=k; } } } LL answer() { if (check(n)) return n; FOR(i,1,n) T[i]=true; FOR(i,2,MIN(n/2,8000)) if (T[i]) eliminate(i); /* eliminate(2); eliminate(3); if (T[4]) eliminate(4); eliminate(5); eliminate(7); if (T[8]) eliminate(8); if (T[9]) eliminate(9); eliminate(11); eliminate(13); if (T[16]) eliminate(16); eliminate(17); */ FORD(k,n,2) if (T[k]) { if (check(k)) return k; } return 1; } void hacePlaya(LL k) { uniform_int_distribution<mt19937::result_type> distrA(0,5); FOR(i,1,n) P[i]=0; FOR(i,1,k) P[i]=1; FOR(i,1,n-k+1) { LL waves=distrA(rng); if (waves>0) { FOR(j,i,i+k-1) P[j]+=waves; } } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); /* n=8; uniform_int_distribution<mt19937::result_type> distrA(3,n-1); set<int> s; while(true) { LL k=distrA(rng); while(k%2==0) k=distrA(rng); hacePlaya(k); //FOR(i,1,n) cout << P[i] << " "; cout << "\n"; cout.flush(); if (check(2)) { LL ans = answer(); if (ans==n-1) { FOR(i,1,n) cout << P[i] << " "; cout << "\n"; cout.flush(); } if (s.count(ans)==0) { s.insert(ans); if (ans%2==1) cout << "!!! "; cout << ans << "\n"; cout.flush(); } } } */ readData(); cout << answer(); return 0; } |
English