Submission #1207572

#TimeUsernameProblemLanguageResultExecution timeMemory
1207572mychecksedadCoin Collecting (JOI19_ho_t4)C++17
8 / 100
6 ms12360 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> const int N = 1500+100, M = 1e5+10, K = 52, MX = 30; const ll INF = 1e18; int n; ll dp[N][N]; array<ll, 2> a[N]; void assertt(bool x){ if(!x){ vi v; for(int i = 0; i < MOD*2; ++i) v.pb(i); } } void solve(){ cin >> n; ll ans = 0; vector<array<int, 2>> pt(n + 1); for(int i = 1; i <= 2*n; ++i){ cin >> a[i][0] >> a[i][1]; if(a[i][0] < 1){ ans += 1 - a[i][0]; a[i][0] = 1; } if(a[i][0] > n){ ans += a[i][0] - n; a[i][0] = n; } if(a[i][1] < 1){ ans += 1 - a[i][1]; a[i][1] = 1; } if(a[i][1] > 2){ ans += a[i][1] - 2; a[i][1] = 2; } pt[a[i][0]][a[i][1] - 1]++; // cerr << a[i][0] << ' ' << a[i][1] << '\n'; } for(int i = 0; i <= n; ++i){ for(int j = 0; j <= n + 1; ++j) dp[i][j] = INF; } dp[0][0] = 0; int cnt = 0; for(int i = 1; i <= n; ++i){ for(int j = 0; j <= n; ++j) dp[i][j] = dp[i - 1][j]; int x = pt[i][1]; int y = pt[i][0]; vector<int> C; if(x <= y){ for(int j = 0; j < x; ++j){ C.pb(1); C.pb(0); } for(int j = 0; j < y - x; ++j) C.pb(1); }else{ for(int j = 0; j < y; ++j){ C.pb(0); C.pb(1); } for(int j = 0; j < x - y; ++j) C.pb(0); } reverse(all(C)); for(int k = 1; k <= x + y; ++k){ int old_down = max(0, cnt / 2 - 5); int old_up = min(n, cnt / 2 + 5); ++cnt; int down = max(0, cnt/2 - 5); int up = min(n, cnt/2 + 5); int tp = C.back(); C.pop_back(); for(int j = up; j >= down; --j){ int cost1 = (!tp ? 1 : 0) + abs(i - (cnt - j)); // moving to down int cost2 = (tp ? 1 : 0) + abs(i - j); // moving to down if(j > old_down && j <= old_up) dp[i][j] = min(dp[i][j] + cost1, dp[i][j - 1] + cost2); else if(j == old_down) dp[i][j] = dp[i][j] + cost1; else dp[i][j] = dp[i][j - 1] + cost2; } } // for(int j = 0; j <= n; ++j){ // cerr << i << ' ' << j << ' ' << cnt/2 << ' ' << dp[i][j] << '\n'; // } // en; // int mn = *min_element(dp[i], dp[i]+n+1); // bool ok = false; // for(int j = max(0, cnt/2 - 2); j <= min(n, cnt/2 + 2); ++j){ // if(mn == dp[i][j]) ok = 1; // } // assertt(ok); } cout << ans + dp[n][n]; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); en; } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...