#include<bits/stdc++.h>
using namespace std;
#define int long long
const int TAILLE_MAX=100*1000+5,INFINI=1000*1000*1000;
int nbVal,posInit,tempsInit;
int val[TAILLE_MAX];
vector<pair<int,int>> decompo(vector<int> v) {
vector<pair<int,int>> ans;
int somCour=0,minSom=0,taille=0;
for (int i:v) {
//cout<<i<<" ";
somCour+=i;
minSom=min(minSom,somCour);
taille++;
if (somCour>=0) {
ans.push_back({somCour,minSom});
taille=0;
somCour=0;
minSom=0;
}
}
if (taille>0) {
ans.push_back({somCour,minSom});
}
/*cout<<" : ";
for (auto i:ans) {
cout<<i.first<<" "<<i.second<<" ";
}
cout<<endl;*/
reverse(ans.begin(),ans.end());
return ans;
}
bool calc(vector<pair<int,int>> bonGau,vector<pair<int,int>> bonDro) {
int somCour=0;
while ((!bonGau.empty() and somCour+bonGau.back().second>=0) or (!bonDro.empty() and somCour+bonDro.back().second>=0)) {
if (!bonGau.empty() and somCour+bonGau.back().second>=0 and bonGau.back().first>=0) {
somCour+=bonGau.back().first;
bonGau.pop_back();
}
else if (!bonDro.empty() and somCour+bonDro.back().second>=0 and bonDro.back().first>=0) {
somCour+=bonDro.back().first;
bonDro.pop_back();
}
else if (bonGau.empty() or bonDro.empty()) {
return true;
}
else if ((int)bonGau.size()>1 or (int)bonDro.size()>1) {
return false;
}
else if ((somCour+bonGau.back().second>=0 and somCour+bonGau.back().first+bonDro.back().second>=0) or
(somCour+bonDro.back().second>=0 and somCour+bonDro.back().first+bonGau.back().second>=0)) {
return true;
}
else {
return false;
}
}
return (bonGau.empty() and bonDro.empty());
}
bool verif(int temps) {
//cout<<temps<<endl;
vector<int> listeGau,listeDro;
for (int i=posInit;i>1;i--) {
listeGau.push_back(2*temps-val[i]+val[i-1]);
}
for (int i=posInit+1;i<=nbVal;i++) {
listeDro.push_back(2*temps-val[i]+val[i-1]);
}
vector<pair<int,int>> bonGau=decompo(listeGau),bonDro=decompo(listeDro);
return calc(bonGau,bonDro);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>nbVal>>posInit>>tempsInit;
for (int i=1;i<=nbVal;i++) {
cin>>val[i];
}
int deb=0,fin=INFINI,mid;
while (deb!=fin) {
mid=(deb+fin)/2;
if (verif(mid)) {
fin=mid;
}
else {
deb=mid+1;
}
}
cout<<(deb+tempsInit-1)/tempsInit<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |