# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
100175 | 2019-03-09T18:24:56 Z | TadijaSebez | Palembang Bridges (APIO15_bridge) | C++11 | 475 ms | 14960 KB |
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define ll long long const int N=100050; const ll inf=9e18; int l[N],r[N]; ll dp[N][2]; int main() { int k,n; scanf("%i %i",&k,&n); char a,b; ll all=0; vector<int> work; for(int i=1;i<=n;i++) { scanf("\n%c %i %c %i",&a,&l[i],&b,&r[i]); if(a!=b) { if(l[i]>r[i]) swap(l[i],r[i]); work.pb(i); } else all+=abs(l[i]-r[i]); } if(work.empty()) return 0*printf("%lld\n",all); sort(work.begin(),work.end(),[&](int x, int y){ return l[x]+r[x]<l[y]+r[y];}); for(int k=0;k<=1;k++) { multiset<int> st; dp[0][k]=abs(r[work[0]]-l[work[0]]); st.insert(l[work[0]]); st.insert(r[work[0]]); auto it=st.begin(); for(int j=1;j<work.size();j++) { int i=work[j]; st.insert(l[i]); st.insert(r[i]); int last=*it; dp[j][k]=dp[j-1][k]; if(l[i]>=*it && r[i]>=*it) it++; else if(l[i]<*it && r[i]<*it) it--,dp[j][k]+=2*abs(last-*it); dp[j][k]+=abs(l[i]-*it)+abs(r[i]-*it); } reverse(work.begin(),work.end()); //for(int i=0;i<work.size();i++) printf("%lld ",dp[i][k]);printf("\n"); } if(k==1) printf("%lld\n",dp[work.size()-1][0]+all+work.size()); else { ll ans=min(dp[work.size()-1][0],dp[work.size()-1][1]); for(int i=0;i<work.size();i++) { ans=min(ans,dp[i][0]+dp[work.size()-i-2][1]); } printf("%lld\n",ans+all+work.size()); } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 384 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 3 ms | 512 KB | Output is correct |
5 | Correct | 4 ms | 512 KB | Output is correct |
6 | Correct | 2 ms | 512 KB | Output is correct |
7 | Correct | 3 ms | 496 KB | Output is correct |
8 | Correct | 3 ms | 384 KB | Output is correct |
9 | Correct | 3 ms | 384 KB | Output is correct |
10 | Correct | 3 ms | 384 KB | Output is correct |
11 | Correct | 3 ms | 384 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 384 KB | Output is correct |
2 | Correct | 2 ms | 256 KB | Output is correct |
3 | Correct | 2 ms | 512 KB | Output is correct |
4 | Correct | 3 ms | 384 KB | Output is correct |
5 | Correct | 3 ms | 256 KB | Output is correct |
6 | Correct | 3 ms | 384 KB | Output is correct |
7 | Correct | 3 ms | 512 KB | Output is correct |
8 | Correct | 2 ms | 512 KB | Output is correct |
9 | Correct | 4 ms | 384 KB | Output is correct |
10 | Correct | 4 ms | 384 KB | Output is correct |
11 | Correct | 3 ms | 512 KB | Output is correct |
12 | Correct | 137 ms | 12628 KB | Output is correct |
13 | Correct | 429 ms | 12636 KB | Output is correct |
14 | Correct | 423 ms | 11432 KB | Output is correct |
15 | Correct | 188 ms | 7668 KB | Output is correct |
16 | Correct | 155 ms | 12656 KB | Output is correct |
17 | Correct | 148 ms | 12656 KB | Output is correct |
18 | Correct | 131 ms | 12588 KB | Output is correct |
19 | Correct | 164 ms | 12564 KB | Output is correct |
20 | Correct | 141 ms | 12528 KB | Output is correct |
21 | Correct | 149 ms | 12548 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 2 ms | 384 KB | Output is correct |
3 | Correct | 2 ms | 356 KB | Output is correct |
4 | Correct | 2 ms | 384 KB | Output is correct |
5 | Correct | 2 ms | 384 KB | Output is correct |
6 | Correct | 3 ms | 256 KB | Output is correct |
7 | Correct | 2 ms | 384 KB | Output is correct |
8 | Correct | 2 ms | 384 KB | Output is correct |
9 | Correct | 2 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 2 ms | 384 KB | Output is correct |
12 | Correct | 2 ms | 384 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 384 KB | Output is correct |
2 | Correct | 2 ms | 384 KB | Output is correct |
3 | Correct | 2 ms | 384 KB | Output is correct |
4 | Correct | 2 ms | 384 KB | Output is correct |
5 | Correct | 2 ms | 384 KB | Output is correct |
6 | Correct | 2 ms | 256 KB | Output is correct |
7 | Correct | 2 ms | 384 KB | Output is correct |
8 | Correct | 2 ms | 384 KB | Output is correct |
9 | Correct | 2 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 2 ms | 384 KB | Output is correct |
12 | Correct | 2 ms | 384 KB | Output is correct |
13 | Correct | 3 ms | 384 KB | Output is correct |
14 | Correct | 3 ms | 512 KB | Output is correct |
15 | Correct | 4 ms | 512 KB | Output is correct |
16 | Correct | 2 ms | 384 KB | Output is correct |
17 | Correct | 3 ms | 408 KB | Output is correct |
18 | Correct | 6 ms | 384 KB | Output is correct |
19 | Correct | 3 ms | 512 KB | Output is correct |
20 | Correct | 3 ms | 512 KB | Output is correct |
21 | Correct | 3 ms | 384 KB | Output is correct |
22 | Correct | 4 ms | 512 KB | Output is correct |
23 | Correct | 4 ms | 512 KB | Output is correct |
24 | Correct | 3 ms | 512 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 256 KB | Output is correct |
2 | Correct | 3 ms | 256 KB | Output is correct |
3 | Correct | 3 ms | 384 KB | Output is correct |
4 | Correct | 2 ms | 384 KB | Output is correct |
5 | Correct | 2 ms | 384 KB | Output is correct |
6 | Correct | 2 ms | 256 KB | Output is correct |
7 | Correct | 2 ms | 384 KB | Output is correct |
8 | Correct | 3 ms | 384 KB | Output is correct |
9 | Correct | 3 ms | 384 KB | Output is correct |
10 | Correct | 2 ms | 384 KB | Output is correct |
11 | Correct | 2 ms | 384 KB | Output is correct |
12 | Correct | 3 ms | 384 KB | Output is correct |
13 | Correct | 4 ms | 512 KB | Output is correct |
14 | Correct | 4 ms | 512 KB | Output is correct |
15 | Correct | 3 ms | 512 KB | Output is correct |
16 | Correct | 2 ms | 384 KB | Output is correct |
17 | Correct | 3 ms | 384 KB | Output is correct |
18 | Correct | 3 ms | 384 KB | Output is correct |
19 | Correct | 4 ms | 512 KB | Output is correct |
20 | Correct | 4 ms | 384 KB | Output is correct |
21 | Correct | 4 ms | 512 KB | Output is correct |
22 | Correct | 4 ms | 512 KB | Output is correct |
23 | Correct | 4 ms | 512 KB | Output is correct |
24 | Correct | 4 ms | 512 KB | Output is correct |
25 | Correct | 143 ms | 13264 KB | Output is correct |
26 | Correct | 315 ms | 13552 KB | Output is correct |
27 | Correct | 475 ms | 14224 KB | Output is correct |
28 | Correct | 413 ms | 14960 KB | Output is correct |
29 | Correct | 438 ms | 14716 KB | Output is correct |
30 | Correct | 172 ms | 8052 KB | Output is correct |
31 | Correct | 151 ms | 14192 KB | Output is correct |
32 | Correct | 152 ms | 14704 KB | Output is correct |
33 | Correct | 137 ms | 14400 KB | Output is correct |
34 | Correct | 201 ms | 14844 KB | Output is correct |
35 | Correct | 150 ms | 14320 KB | Output is correct |
36 | Correct | 153 ms | 14576 KB | Output is correct |
37 | Correct | 143 ms | 13428 KB | Output is correct |