Submission #899755

# Submission time Handle Problem Language Result Execution time Memory
899755 2024-01-07T00:38:24 Z nightfal Wiring (IOI17_wiring) C++14
55 / 100
25 ms 5792 KB
// #include "wiring.h"
#include <cassert>
#include <cstdio>
#include <vector>
#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<n || j<m;)
        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 > 6) return false;
    }
  return true;
}
bool isSubtask4(std::vector<int> r, std::vector<int> b) {
    return min(r[0],b[0])==1 && max(r[r.size()-1],b[b.size()-1])==r.size()+b.size();
}
 
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 subtask4(std::vector<int>, std::vector<int>);
long long min_total_length(std::vector<int>, std::vector<int> );
 
long long min_total_length(std::vector<int> r, std::vector<int> b) {
    if (isSubtask4(r,b)) return subtask4(r,b);
    if (isSubtask3(r,b)) return subtask3(r,b);
  	else if (isSubtask2(r,b)) return subtask2(r,b,0,r.size()-1,0,b.size()-1);    
  	else if (isSubtask1(r,b)) return subtask1(r,b);
//     else return fullTask(r,b);
    return 0;
}

long long subtask4(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<n || j<m;)
        i==n? a[i+j++]=1: j==m? a[j+i++]=0: r[i]<b[j]? a[j+i++]=0: a[i+j++]=1;

    long long dp1=LLONG_MAX, dp2=LLONG_MAX;
    long long cntL=0, sumL=0, sumL2=0, cntR=1, sumR=0, sumR2=0;
    int prev=-1, curr=0; 

    for(int i=1; i<n+m; i++) {
        if (a[i]!=a[i-1]) {prev=curr; curr=i; cntL=cntR, cntR=0;}
        cntR++;
        if(i==n+m-1) {
            if(prev==0) {
                sumL = cntL*(cntL-1)/2; sumR = (cntR)*(cntR-1)/2; 
                return sumL + sumR + max(cntL,cntR);
            }
            else {
                sumL = (cntL/2)*(cntL/2-1)/2; sumL2 = ((cntL+1)/2)*((cntL+1)/2-1)/2;
                sumR = (cntR)*(cntR-1)/2;
                long long dp = sumL + dp2 + sumR + max(cntL/2,cntR);
                if(dp1 < LLONG_MAX) dp = min(dp, sumL2 + dp1 + sumR + max((cntL+1)/2,cntR));
                return dp;
            }
        }
        else if (a[i]!= a[i+1]) { 
            if (prev==0) {
                sumL = cntL*(cntL-1)/2; 
                sumR = (cntR/2)*(cntR/2-1)/2; sumR2 = ((cntR+1)/2)*((cntR+1)/2-1)/2;
                // if (cntR!=1) 
                dp1 = sumL + sumR + max(cntL,cntR/2); 
                dp2 = sumL + sumR2 + max(cntL,(cntR+1)/2);            
            }
            else if (prev>0) {
                sumL = (cntL/2)*(cntL/2-1)/2; sumL2 = ((cntL+1)/2)*((cntL+1)/2-1)/2;
                sumR = (cntR/2)*(cntR/2-1)/2; sumR2 = ((cntR+1)/2)*((cntR+1)/2-1)/2;
                long long new_dp1 = sumL + dp2 + sumR  + max(cntL/2,cntR/2);
                long long new_dp2 = sumL + dp2 + sumR2 + max(cntL/2,(cntR+1)/2);
                // if (dp1<LLONG_MAX) {
                // if (cntL!=1) {
                    new_dp1 = min(new_dp1, sumL2 + dp1 + sumR  + max((cntL+1)/2,cntR/2));
                    new_dp2 = min(new_dp2, sumL2 + dp1 + sumR2 + max((cntL+1)/2,(cntR+1)/2));
                // }
                dp1 = new_dp1; dp2 = new_dp2;
            }
            // printf("sumL=%lld, sumL2=%lld, sumR=%lld, sumR2=%lld\n",sumL,sumL2,sumR,sumR2);
            // printf("dp1=%lld, dp2=%lld\n",dp1,dp2);
        }
    }
}

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;) // make_pair 내부에서 ++ 하면 안됨
        i==n? a[i+j++]=make_pair(j,1): j==m? a[j+i++]=make_pair(i,0): r[i]<b[j]? a[j+i++]=make_pair(i,0): a[i+j++]=make_pair(j,1);

    vector<long long> dp(n+m,LLONG_MAX);

    long long sumL=0, cntL=0, sumR=0, cntR=0, gap=0;
    int prev=-1, curr=0;

    for(int i=1; i<n+m; i++) {
        if (a[i].second != a[i-1].second) {
            prev=curr; curr=i; sumR=cntR=0;
            gap= a[prev].second==0? b[a[curr].first]-r[a[curr-1].first]: r[a[curr].first]-b[a[curr-1].first];
        }
        if (prev==-1) continue;
        sumR += a[curr].second==0? r[a[i].first]-r[a[curr].first]:b[a[i].first]-b[a[curr].first];
        cntR++;
        for(int j=curr-1; j>=prev; j--) {
            cntL++; 
            sumL += a[prev].second==0? r[a[curr-1].first]-r[a[j].first]:b[a[curr-1].first]-b[a[j].first]; 
            long long temp = sumL + sumR + max(cntL,cntR)*gap;
            if (j==0) dp[i] = min(dp[i],temp);
            else {
                long long minDP = min(dp[j-1],dp[j]);        
                if (minDP == LLONG_MAX) continue;
                else temp += minDP;                    
                if (dp[i]>temp) dp[i]=temp;
            }
            // printf("dp[%d,%d]=%d, sumL=%d, sumR=%d, gap=%d\n",i,j,dp[i],sumL,sumR,gap);
        }
        sumL=cntL=0;
    }
    return dp[n+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 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];
}

Compilation message

wiring.cpp: In function 'bool isSubtask4(std::vector<int>, std::vector<int>)':
wiring.cpp:28:65: warning: comparison of integer expressions of different signedness: 'const int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     return min(r[0],b[0])==1 && max(r[r.size()-1],b[b.size()-1])==r.size()+b.size();
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
wiring.cpp: In function 'long long int subtask1(std::vector<int>, std::vector<int>)':
wiring.cpp:151:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  151 |       for(int j=1; j<m; j++)
      |       ^~~
wiring.cpp:154:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  154 |  return dp[n-1][m-1];
      |  ^~~~~~
wiring.cpp: In function 'long long int subtask4(std::vector<int>, std::vector<int>)':
wiring.cpp:48:22: warning: control reaches end of non-void function [-Wreturn-type]
   48 |     vector<int> a(n+m);
      |                      ^
# 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 348 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 1 ms 600 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 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 1 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 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 13 ms 2540 KB Output is correct
4 Correct 13 ms 2576 KB Output is correct
5 Correct 14 ms 2740 KB Output is correct
6 Correct 18 ms 3400 KB Output is correct
7 Correct 17 ms 3656 KB Output is correct
8 Correct 18 ms 3404 KB Output is correct
9 Correct 18 ms 3500 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 Correct 25 ms 5792 KB Output is correct
4 Correct 23 ms 5692 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 380 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 25 ms 5696 KB Output is correct
19 Correct 25 ms 5696 KB Output is correct
20 Correct 24 ms 5792 KB Output is correct
21 Correct 25 ms 5656 KB Output is correct
22 Correct 25 ms 5688 KB Output is correct
23 Correct 25 ms 5692 KB Output is correct
24 Correct 25 ms 5692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 16 ms 3404 KB Output is correct
3 Correct 16 ms 3708 KB Output is correct
4 Correct 16 ms 3404 KB Output is correct
5 Correct 17 ms 3404 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 25 ms 3404 KB Output is correct
19 Correct 16 ms 3404 KB Output is correct
20 Correct 16 ms 3500 KB Output is correct
21 Correct 20 ms 3400 KB Output is correct
22 Correct 16 ms 3404 KB Output is correct
23 Correct 16 ms 3504 KB Output is correct
24 Correct 16 ms 3400 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 Correct 0 ms 348 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 1 ms 600 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 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 1 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 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 13 ms 2540 KB Output is correct
20 Correct 13 ms 2576 KB Output is correct
21 Correct 14 ms 2740 KB Output is correct
22 Correct 18 ms 3400 KB Output is correct
23 Correct 17 ms 3656 KB Output is correct
24 Correct 18 ms 3404 KB Output is correct
25 Correct 18 ms 3500 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 25 ms 5792 KB Output is correct
29 Correct 23 ms 5692 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 380 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 600 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 25 ms 5696 KB Output is correct
44 Correct 25 ms 5696 KB Output is correct
45 Correct 24 ms 5792 KB Output is correct
46 Correct 25 ms 5656 KB Output is correct
47 Correct 25 ms 5688 KB Output is correct
48 Correct 25 ms 5692 KB Output is correct
49 Correct 25 ms 5692 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 16 ms 3404 KB Output is correct
52 Correct 16 ms 3708 KB Output is correct
53 Correct 16 ms 3404 KB Output is correct
54 Correct 17 ms 3404 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 344 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 25 ms 3404 KB Output is correct
68 Correct 16 ms 3404 KB Output is correct
69 Correct 16 ms 3500 KB Output is correct
70 Correct 20 ms 3400 KB Output is correct
71 Correct 16 ms 3404 KB Output is correct
72 Correct 16 ms 3504 KB Output is correct
73 Correct 16 ms 3400 KB Output is correct
74 Incorrect 18 ms 3404 KB 3rd lines differ - on the 1st token, expected: '26103122711157', found: '0'
75 Halted 0 ms 0 KB -