# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
896276 |
2024-01-01T07:32:49 Z |
nightfal |
Wiring (IOI17_wiring) |
C++14 |
|
0 ms |
0 KB |
#include "wiring.h"
#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;)
i==n? a[i+j++]=1: j==m? a[j+i++]=0: r[i]<b[j]? a[j+i++] = 0:a[i+j++] = 1;
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);
}
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()-1, m = b.size()-1;
vector<make_tuple<int,int,int>> a(n+m+1);
for(int i=0, j-=0; i<n || j<m;) {
if (i==n) a[i+j] = make_tuple(j++,1);
else if (j==m) a[i+j] = make_pair(i++,0);
else if (r[i]<b[j]) a[i+j] = make_pair(i++,0);
else a[i+j] = make_pair(j++,1);
}
a[n+m] = make_pair(0,2);
vector<long long> dp(n+m);
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;
dp[curr] = a[0].second==0? subtask2(r,b,prev,curr-1,curr,curr):subtask2(b,r,prev,curr-1,curr,curr);
int start = curr+1;
for(int i=start; i<n+m+1; i++) {
if (a[i].second != a[i-1].second) {prev = curr; curr = i;}
for(int j =prev; j<curr; j++) {
long long temp = a[prev].second==0? subtask2(r,b,prev,curr-1,curr,i):subtask2(b,r,0,curr-1,curr,i);
if (dp[i]>temp) dp[i] = temp;
}
}
return dp[n+m-1];
}
Compilation message
wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:29:14: error: 'isSubtask4' was not declared in this scope; did you mean 'isSubtask3'?
29 | else if (isSubtask4(r,b)) return subtask4(r,b);
| ^~~~~~~~~~
| isSubtask3
wiring.cpp:29:38: error: 'subtask4' was not declared in this scope; did you mean 'subtask3'?
29 | else if (isSubtask4(r,b)) return subtask4(r,b);
| ^~~~~~~~
| subtask3
wiring.cpp:30:17: error: 'fullTask' was not declared in this scope
30 | else return fullTask(r,b);
| ^~~~~~~~
wiring.cpp: In function 'long long int subtask1(std::vector<int>, std::vector<int>)':
wiring.cpp:40:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
40 | for(int j=1; j<m; j++)
| ^~~
wiring.cpp:42:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
42 | return dp[n-1][m-1];
| ^~~~~~
wiring.cpp: In function 'long long int subtask3(std::vector<int>, std::vector<int>)':
wiring.cpp:54:12: error: 'make_tuple' was not declared in this scope
54 | vector<make_tuple<int,int,int>> a(n+m+1);
| ^~~~~~~~~~
wiring.cpp:4:1: note: 'std::make_tuple' is defined in header '<tuple>'; did you forget to '#include <tuple>'?
3 | #include <algorithm>
+++ |+#include <tuple>
4 | using namespace std;
wiring.cpp:54:34: error: template argument 1 is invalid
54 | vector<make_tuple<int,int,int>> a(n+m+1);
| ^~
wiring.cpp:54:34: error: template argument 2 is invalid
wiring.cpp:56:19: error: expected ';' before '-=' token
56 | for(int i=0, j-=0; i<n || j<m;) {
| ^~
| ;
wiring.cpp:56:19: error: expected primary-expression before '-=' token
56 | for(int i=0, j-=0; i<n || j<m;) {
| ^~
wiring.cpp:56:31: error: 'j' was not declared in this scope
56 | for(int i=0, j-=0; i<n || j<m;) {
| ^
wiring.cpp:56:34: error: expected ')' before ';' token
56 | for(int i=0, j-=0; i<n || j<m;) {
| ~ ^
| )
wiring.cpp:56:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
56 | for(int i=0, j-=0; i<n || j<m;) {
| ^~~
wiring.cpp:56:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
56 | for(int i=0, j-=0; i<n || j<m;) {
| ^
wiring.cpp:56:35: error: expected primary-expression before ')' token
wiring.cpp:62:6: error: invalid types 'int[int]' for array subscript
62 | a[n+m] = make_pair(0,2);
| ^
wiring.cpp:66:19: error: 'curr' was not declared in this scope
66 | int prev = 0; curr;
| ^~~~
wiring.cpp:71:17: error: invalid types 'int[int]' for array subscript
71 | dp[curr] = a[0].second==0? subtask2(r,b,prev,curr-1,curr,curr):subtask2(b,r,prev,curr-1,curr,curr);
| ^
wiring.cpp:74:14: error: invalid types 'int[int]' for array subscript
74 | if (a[i].second != a[i-1].second) {prev = curr; curr = i;}
| ^
wiring.cpp:74:29: error: invalid types 'int[int]' for array subscript
74 | if (a[i].second != a[i-1].second) {prev = curr; curr = i;}
| ^
wiring.cpp:76:31: error: invalid types 'int[int]' for array subscript
76 | long long temp = a[prev].second==0? subtask2(r,b,prev,curr-1,curr,i):subtask2(b,r,0,curr-1,curr,i);
| ^