Submission #2147906
Source Code Expand
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define RREP(i, a, b) for(int i = (a); i >= (b); i--)
#define pq priority_queue
#define P pair<int, int>
#define P2 pair<int, P>
#define P3 pair<int, P2>
typedef long long ll; typedef long double ld;
using namespace std;
const int INF = 1e9, MOD = 1e9+7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
const int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
const ld PI = abs(acos(-1));
const int sqrtN = 512;
const int logN = 32;
const ll LINF=1e18;
vector<int> vec;
struct Node{
int ma, sum;
Node(int ma, int sum) : ma(ma), sum(sum) {}
Node operator*(const Node r) const{return Node(max(ma, r.ma), sum + r.sum);}
} e(0, 0);
struct SegmentTree{
vector<Node> seg;
int sz = 1;
SegmentTree(int n){
while(sz < n) sz *= 2;
seg.assign(sz * 2, e);
}
void add(int k){
int tmp = vec[k];
k += sz - 1;
seg[k] = Node(tmp, seg[k].sum + 1);
while(k){
k = (k - 1) / 2;
seg[k] = seg[2 * k + 1] * seg[2 * k + 2];
}
}
Node get(int a, int b){
return get(a, b, 0, 0, sz);
}
Node get(int a, int b, int k, int l, int r){
if(b<=l||r<=a) return e;
if(a<=l&&r<=b) return seg[k];
return get(a, b, 2 * k + 1, l, (l + r) / 2) * get(a, b, 2 * k + 2, (l + r) / 2, r);
}
};
int main(){
int n; cin >> n;
int a[200010]; for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) vec.push_back(a[i]);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
SegmentTree seg(vec.size());
int pos = lower_bound(vec.begin(), vec.end(), a[0]) - vec.begin();
seg.add(pos);
int dp[200010] = {};
for(int i = 1; i < n; i++){
int k = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
int pos = max(0, seg.get(0, vec.size()).ma - a[i]);
int pot = seg.get(k + 1, vec.size()).sum;
if(pos < pot){//increment
if(pos){
dp[i] = dp[i - 1] + pos;
int tmp = seg.get(0, vec.size()).ma;
k = lower_bound(vec.begin(), vec.end(), tmp) - vec.begin();
seg.add(k);
}else{
dp[i] = dp[i - 1] + pos;
seg.add(k);
}
}else{//swap
dp[i] = dp[i - 1] + pot;
seg.add(k);
}
}
//~ for(int i = 0; i < n; i++) cout << dp[i] << " "; cout << endl;
cout << dp[n - 1] << endl;
return 0;
}
//dp[i + 1] = min(dp[i] + max(0, a[i - 1] - a[i]), dp[i] + (i + 1 以上の個数));
//~ 0 1 1 3
//~ 2 7 8 9 4 6 7 9 7 4 7 4 4 3 6 2 3 4 4 9
Submission Info
Submission Time |
|
Task |
B - Increment and Swap |
User |
ecasdqina |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2500 Byte |
Status |
WA |
Exec Time |
195 ms |
Memory |
6772 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 1500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0.txt, example1.txt |
All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, example0.txt, example1.txt |
Case Name |
Status |
Exec Time |
Memory |
000.txt |
WA |
175 ms |
4724 KB |
001.txt |
WA |
177 ms |
4724 KB |
002.txt |
WA |
177 ms |
4724 KB |
003.txt |
WA |
179 ms |
4724 KB |
004.txt |
WA |
182 ms |
4724 KB |
005.txt |
WA |
177 ms |
4724 KB |
006.txt |
WA |
179 ms |
4724 KB |
007.txt |
WA |
181 ms |
4724 KB |
008.txt |
WA |
174 ms |
4724 KB |
009.txt |
WA |
178 ms |
4724 KB |
010.txt |
WA |
164 ms |
4724 KB |
011.txt |
WA |
180 ms |
6772 KB |
012.txt |
WA |
182 ms |
6772 KB |
013.txt |
WA |
180 ms |
6772 KB |
014.txt |
WA |
183 ms |
6772 KB |
015.txt |
WA |
185 ms |
6772 KB |
016.txt |
WA |
184 ms |
6772 KB |
017.txt |
WA |
182 ms |
6772 KB |
018.txt |
WA |
161 ms |
6772 KB |
019.txt |
WA |
175 ms |
6772 KB |
020.txt |
WA |
180 ms |
6772 KB |
021.txt |
WA |
18 ms |
1280 KB |
022.txt |
WA |
174 ms |
4724 KB |
023.txt |
WA |
81 ms |
2936 KB |
024.txt |
WA |
31 ms |
2044 KB |
025.txt |
WA |
96 ms |
3192 KB |
026.txt |
WA |
21 ms |
1664 KB |
027.txt |
WA |
133 ms |
4340 KB |
028.txt |
WA |
67 ms |
2808 KB |
029.txt |
WA |
61 ms |
2808 KB |
030.txt |
WA |
65 ms |
2808 KB |
031.txt |
WA |
130 ms |
4340 KB |
032.txt |
WA |
84 ms |
2936 KB |
033.txt |
WA |
82 ms |
3960 KB |
034.txt |
WA |
144 ms |
4468 KB |
035.txt |
WA |
26 ms |
1920 KB |
036.txt |
WA |
161 ms |
6644 KB |
037.txt |
WA |
123 ms |
4340 KB |
038.txt |
WA |
34 ms |
2044 KB |
039.txt |
WA |
90 ms |
4088 KB |
040.txt |
WA |
83 ms |
3960 KB |
041.txt |
WA |
166 ms |
6644 KB |
042.txt |
AC |
124 ms |
6772 KB |
043.txt |
WA |
124 ms |
6772 KB |
044.txt |
WA |
171 ms |
6772 KB |
045.txt |
AC |
2 ms |
1024 KB |
046.txt |
WA |
55 ms |
2676 KB |
047.txt |
WA |
195 ms |
6772 KB |
048.txt |
WA |
65 ms |
2676 KB |
049.txt |
WA |
68 ms |
2676 KB |
050.txt |
WA |
85 ms |
2676 KB |
051.txt |
WA |
108 ms |
2676 KB |
052.txt |
WA |
138 ms |
2676 KB |
053.txt |
WA |
158 ms |
3060 KB |
054.txt |
WA |
188 ms |
4724 KB |
055.txt |
WA |
62 ms |
2676 KB |
056.txt |
WA |
66 ms |
2676 KB |
057.txt |
WA |
82 ms |
2676 KB |
058.txt |
WA |
105 ms |
2676 KB |
059.txt |
WA |
134 ms |
2676 KB |
060.txt |
WA |
166 ms |
3060 KB |
061.txt |
WA |
183 ms |
4724 KB |
062.txt |
AC |
133 ms |
4724 KB |
063.txt |
WA |
141 ms |
4724 KB |
example0.txt |
AC |
2 ms |
1024 KB |
example1.txt |
AC |
2 ms |
1024 KB |