Submission #539446

#TimeUsernameProblemLanguageResultExecution timeMemory
539446Killer2501Coin Collecting (JOI19_ho_t4)C++14
100 / 100
54 ms6120 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 1e5+5;
const int mod = 1e9+7;
const ll base = 75;
const int inf = 1e9+7;
int a[N], b[N], dp[N][3], lst[N];
int n, m, q;
ll ans;
void sol()
{
    cin >> n;
    for(int i = 1; i <= n*2; i ++)
    {
        int x, y;
        cin >> x >> y;
        if(x > n)
        {
            ans += x-n;
            x = n;
        }
        if(x < 1)
        {
            ans += 1-x;
            x = 1;
        }
        if(y > 2)
        {
            ans += y-2;
            y = 2;
        }
        if(y < 1)
        {
            ans += 1-y;
            y = 1;
        }
        ++dp[x][y];
        if(i <= n)
        {
            --dp[i][1];
            --dp[i][2];
        }
    }
    for(int i = 1; i <= n; i ++)
    {
        if(dp[i][1] < 0 && dp[i][2] > 0)
        {
            m = min(-dp[i][1], dp[i][2]);
            ans += m;
            dp[i][1] += m;
            dp[i][2] -= m;
        }
        if(dp[i][1] > 0 && dp[i][2] < 0)
        {
            m = min(dp[i][1], -dp[i][2]);
            ans += m;
            dp[i][1] -= m;
            dp[i][2] += m;
        }
        ans += abs(dp[i][1])+abs(dp[i][2]);
        dp[i+1][1] += dp[i][1];
        dp[i+1][2] += dp[i][2];
    }
    cout << ans;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
    int test = 1;
    //cin >> test;
    while(test -- > 0)sol();
    return 0;
}


Compilation message (stderr)

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