QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#61165 | #149. Peru | ltunjic | 0 | 2ms | 8172kb | C++14 | 3.3kb | 2022-11-11 07:15:34 | 2024-09-10 16:33:25 |
Judging History
answer
#include <bits/stdc++.h>
#include "peru.h"
using namespace std;
typedef long long ll;
const int N = 25 * 1e3 + 10;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
ll dp[N];
int add(int a, int b){
if(a + b < 0) return a + b + N;
if(a + b >= N) return a + b - N;
return a + b;
}
ll MODadd(ll a, ll b){
return (a + b) % MOD;
}
ll MODmult(ll a, ll b){
return a * b % MOD;
}
struct elem{
ll val, maxi;
int ind;
elem(){
val = 0;
maxi = 0;
ind = 0;
};
};
struct monostack{
elem arr[N];
int ind = 0;
void push(elem x){
if(ind) x.val = min(x.val, arr[ind - 1].val);
arr[ind] = x;
ind++;
};
elem pop(){
if(ind){
ind--;
return arr[ind];
}
assert(false);
return arr[ind];
};
elem front(){
if(ind) return arr[ind - 1];
assert(false);
return arr[ind];
};
ll mini(){
if(ind) return arr[ind - 1].val;
return INF;
}
int size(){
return ind;
};
void clear(){
ind = 0;
};
};
struct monodeck{
monostack l, r;
elem arr[N];
int L = 0, R = 1;
void build(){
if(l.size() > 0 && r.size() > 0) return;
//printf("building\n");
int mi = (L + R) >> 1;
if(L > R) mi = (L - N + R) >> 1;
l.clear();
r.clear();
for(int i = mi; i != L; i = add(i, -1)){
assert(i >= 0);
l.push(arr[i]);
}
for(int i = mi + 1; i != R; i = add(i, 1)){
assert(i < N);
r.push(arr[i]);
}
}
void push_front(elem x){
arr[L] = x;
l.push(x);
L = add(L, -1);
build();
};
void push_back(elem x){
arr[R] = x;
r.push(x);
R = add(R, 1);
build();
};
elem pop_front(){
if(l.size() == 0) swap(l, r);
L = add(L, 1);
elem ret = l.pop();
build();
return ret;
};
elem pop_back(){
if(r.size() == 0) swap(l, r);
R = add(R, -1);
elem ret = r.pop();
build();
return ret;
};
elem front(){
return arr[add(L, 1)];
};
elem back(){
return arr[add(R, -1)];
};
ll mini(){
return min(l.mini(), r.mini());
}
int size(){
return l.size() + r.size();
};
};
ll DP(int x){
if(x < 0) return 0;
return dp[x];
}
int solve(int n, int k, int* arr){
elem res, p;
monodeck s;
dp[0] = arr[0];
p.val = arr[0];
p.maxi = arr[0];
p.ind = 0;
s.push_back(p);
for(int i = 1; i < n; i++){
if(i >= k){
res = s.pop_front();
//printf("ind = %d\n", res.ind);
p.val = dp[i - k] + res.maxi;
p.maxi = res.maxi;
p.ind = i - k + 1;
if(p.ind != i){
if(s.size()){
if(s.front().ind != p.ind) s.push_front(p);
} else {
s.push_front(p);
}
//if(i == 2) printf("i = 2 ! %d\n", s.size());
}
}
bool del = false;
while(s.size() && s.back().maxi <= arr[i]){
res = s.pop_back();
del = true;
}
//if(i == 3) printf("siz = %d maxi %lld front %d\n", s.size(), s.mini(), s.L);
if(del){
p.val = DP(res.ind - 1) + arr[i];
p.maxi = arr[i];
p.ind = res.ind;
s.push_back(p);
//printf("%d - diff %d\n", i, res.ind);
} else {
p.val = arr[i] + dp[i - 1];
p.maxi = arr[i];
p.ind = i;
s.push_back(p);
//printf("%d - same\n", i);
}
dp[i] = s.mini();
}
ll ans = 0;
ll c = 1;
for(int i = n - 1; i >= 0; i--){
ans = MODadd(ans, MODmult(dp[i], c));
c = MODmult(c, 23);
//printf("%lld ", dp[i]);
}
//printf("\n");
return ans;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 18
Accepted
time: 2ms
memory: 6200kb
input:
2000 170 1054018657 1037445664 1011691297 1009972317 1006506677 1002579733 999792775 999477541 975467893 970302369 968173111 957735623 953086083 938540451 932313113 930563895 924682633 917831575 913506401 908739591 905368525 899452913 894354220 890127447 885923007 583391543 880642788 878397752 87822...
output:
559335223
result:
ok single line: '559335223'
Test #2:
score: 0
Wrong Answer
time: 0ms
memory: 8172kb
input:
2000 100 1019222751 1012312953 1005125581 978875481 967275037 967176717 965763637 960999157 957233281 956958729 953471924 942217905 936256081 925906257 922808341 918616993 918132100 915435367 911342035 909313549 902957201 902119531 900836861 99996937 892982026 884892282 884816596 884102145 883049077...
output:
18827948
result:
wrong answer 1st lines differ - expected: '818524087', found: '18827948'
Subtask #2:
score: 0
Runtime Error
Test #15:
score: 0
Runtime Error
input:
400000 1000 1999989721 1999987224 1999984551 1999977673 1999977545 1999976801 1999975837 1999972607 1999956301 1999952801 1999942489 1999940593 1999940337 1999936353 1999936273 1999926073 1999925513 1999922980 1999918301 1999912501 1999909301 1999906125 1999902913 1999895622 1999893617 1999885490 19...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #34:
score: 0
Runtime Error
input:
2500000 2000 1073315871 1073250349 1072791751 1072104046 1072071097 1071841833 1071809381 1071710686 1071580105 1071482003 1071383725 1071154701 1070499431 1070335288 1070334157 1069943617 1069681476 1069584279 1069581771 1069322519 1069189353 1069125955 1068832186 1068797487 1068662939 1068565681 1...