# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
896320 |
2024-01-01T09:09:30 Z |
nightfal |
Wiring (IOI17_wiring) |
C++14 |
|
1000 ms |
7724 KB |
#include "wiring.h"
#include <climits>
#include <stdlib.h>
#include <algorithm>
using namespace std;
bool isSubtask1(std::vector<int> r, std::vector<int> b) {return r.size()<=200 && b.size()<=200;}
bool isSubtask2(std::vector<int> r, std::vector<int> b) {return r[r.size()-1]<b[0];}
bool isSubtask3(std::vector<int> r, std::vector<int> b) {
// int n=r.size(), m=b.size();
// vector<int> a(n+m);
// for(int i=0, j=0; i<m || j<n;)
// if (i==n) {a[i+j]=1; j++;}
// else if(j==m) {a[j+i]=0; i++;}
// else if (r[i]<b[j]) {a[j+i] = 0; i++;}
// else {a[i+j] = 1; j++;}
// int cnt = 1;
// for(int k=1; k<n+m; k++) {
// cnt = a[k]==a[k-1]? cnt+1:0;
// if (cnt > 7) return false;
// }
return true;
}
long long subtask1(std::vector<int>, std::vector<int>);
long long subtask2(std::vector<int>, std::vector<int>, int, int, int, int);
long long subtask3(std::vector<int>, std::vector<int>);
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if (isSubtask1(r,b)) return subtask1(r,b);
if (isSubtask2(r,b)) return subtask2(r,b,0,r.size()-1,0,b.size()-1);
else if (isSubtask3(r,b)) return subtask3(r,b);
// else if (isSubtask4(r,b)) return subtask4(r,b);
// else return fullTask(r,b);
return 0;
}
long long subtask1(std::vector<int> r, std::vector<int> b) {
int n = r.size(), m = b.size();
long long dp[n][m];
dp[0][0] = abs(r[0]-b[0]);
for(int i=1; i<n; i++) dp[i][0] = dp[i-1][0] + abs(r[i]-b[0]);
for(int j=1; j<m; j++) dp[0][j] = dp[0][j-1] + abs(r[0]-b[j]);
for(int i=1; i<n; i++)
for(int j=1; j<m; j++)
dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]) + abs(r[i]-b[j]);
return dp[n-1][m-1];
}
long long subtask2(std::vector<int> arr1, std::vector<int> arr2, int s1, int e1, int s2, int e2) {
long long sum1=0, sum2=0, n = e1-s1+1, m=e2-s2+1;
for(int i=s1; i<=e1; i++) sum1 += arr1[e1] - arr1[i];
for(int i=s2; i<=e2; i++) sum2 += arr2[i] - arr2[s2];
return sum1+sum2+max(n,m)*(arr2[s2]-arr1[e1]);
}
long long subtask3(std::vector<int> r, std::vector<int> b) {
int n = r.size(), m = b.size();
vector<pair<int,int>> a(n+m);
for(int i=0, j=0; i<n || j<m;) {
if (i==n) {a[i+j] = make_pair(j,1); j++;}
else if (j==m) {a[i+j] = make_pair(i,0); i++;}
else if (r[i]<b[j]) {a[i+j] = make_pair(i,0); i++;}
else {a[i+j] = make_pair(j,1); j++;}
}
vector<long long> dp(n+m,LLONG_MAX);
dp[0] = 0;
int prev = 0, curr;
for(curr=1; curr<n+m; curr++)
if (a[curr].second != a[curr-1].second) break;
else dp[curr] = 0;
if (a[0].second==0)
dp[curr] = subtask2(r,b,a[prev].first,a[curr-1].first,a[curr].first,a[curr].first);
else
dp[curr] = subtask2(b,r,a[prev].first,a[curr-1].first,a[curr].first,a[curr].first);
int start = curr+1;
for(int i=start; i<n+m; i++) {
if (a[i].second != a[i-1].second) {prev = curr; curr = i;}
for(int j =prev; j<curr; j++) {
long long temp;
if (a[prev].second==0)
temp = subtask2(r,b,a[j].first,a[curr-1].first,a[curr].first,a[i].first);
else
temp = subtask2(b,r,a[j].first,a[curr-1].first,a[curr].first,a[i].first);
if (dp[i]>temp+dp[j-1]) dp[i] = temp+dp[j-1];
}
}
return dp[n+m-1];
}
Compilation message
wiring.cpp: In function 'long long int subtask1(std::vector<int>, std::vector<int>)':
wiring.cpp:46:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
46 | for(int j=1; j<m; j++)
| ^~~
wiring.cpp:49:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
49 | return dp[n-1][m-1];
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
15 ms |
2048 KB |
Output is correct |
4 |
Correct |
13 ms |
2140 KB |
Output is correct |
5 |
Correct |
12 ms |
2140 KB |
Output is correct |
6 |
Correct |
16 ms |
2756 KB |
Output is correct |
7 |
Correct |
18 ms |
2752 KB |
Output is correct |
8 |
Correct |
16 ms |
2748 KB |
Output is correct |
9 |
Correct |
16 ms |
2752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1077 ms |
7724 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1082 ms |
6840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
15 ms |
2048 KB |
Output is correct |
20 |
Correct |
13 ms |
2140 KB |
Output is correct |
21 |
Correct |
12 ms |
2140 KB |
Output is correct |
22 |
Correct |
16 ms |
2756 KB |
Output is correct |
23 |
Correct |
18 ms |
2752 KB |
Output is correct |
24 |
Correct |
16 ms |
2748 KB |
Output is correct |
25 |
Correct |
16 ms |
2752 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Execution timed out |
1077 ms |
7724 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |