Submission #1145316

#TimeUsernameProblemLanguageResultExecution timeMemory
1145316SmuggingSpunCoin Collecting (JOI19_ho_t4)C++20
100 / 100
82 ms8520 KiB
#include<bits/stdc++.h>
#define taskname "D"
using namespace std;
typedef long long ll;
const ll INF = 1e18;
template<class T>void minimize(T& a, T b){
	if(a > b){
		a = b;
	}
}
int n;
namespace sub1{
	void solve(){
		vector<pair<int, int>>a(n << 1);
		for(auto& [x, y] : a){
			cin >> x >> y;
		}
		vector<ll>dp(1 << (n << 1), INF);
		dp[0] = 0;
		for(int mask = 1; mask < (1 << (n << 1)); mask++){
			int cnt = __builtin_popcount(mask);
			for(int i = 0; i < (n << 1); i++){
				if(1 << i & mask){
					if(cnt > n){
						minimize(dp[mask], dp[mask ^ (1 << i)] + abs(a[i].first - cnt + n) + abs(a[i].second - 2));
					}
					else{
						minimize(dp[mask], dp[mask ^ (1 << i)] + abs(a[i].first - cnt) + abs(a[i].second - 1));
					}
				}
			}
		}
		cout << dp.back();
	}
}
namespace sub23{
	void solve(){
		vector<vector<int>>cnt(n + 1, vector<int>(2, 0));
		ll ans = 0;
		for(int _ = 0; _ < (n << 1); _++){
			int x, y;
			cin >> x >> y;
			if(x < 1){
				ans += 1 - x;
				x = 1;
			}
			else if(x > n){
				ans += x - n;
				x = n;
			}
			if(y < 1){
				ans += 1 - y;
				y = 1;
			}
			else if(y > 2){
				ans += y - 2;
				y = 2;
			}
			cnt[x][y - 1]++;
		}
		for(int i = 1; i <= n; i++){
			cnt[i][0] += cnt[i - 1][0] - 1;
			cnt[i][1] += cnt[i - 1][1] - 1;
			for(int j = 0; j < 2; j++){
				if(cnt[i][j] > 0 && cnt[i][j ^ 1] < 0){
					int d = min(cnt[i][j], -cnt[i][j ^ 1]);
					ans += d;
					cnt[i][j] -= d;
					cnt[i][j ^ 1] += d;
				}
			}
			ans += abs(cnt[i][0]) + abs(cnt[i][1]);
		}
		cout << ans;
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n;
	if(n <= 10){
		sub1::solve();
	}
	else{
		sub23::solve();
	}
}

Compilation message (stderr)

joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:80:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...