# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
145272 | TadijaSebez | Coin Collecting (JOI19_ho_t4) | C++11 | 49 ms | 3576 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100050;
const int M=2050;
const int inf=1e9+7;
int pos[2][M],sz[2],cnt[N][2],dp[M],tmp[M];
void Clear(int a[]){ for(int i=0;i<M;i++) a[i]=inf;}
void ckmn(int &a, int b){ a=min(a,b);}
int main()
{
int n,x,y;
scanf("%i",&n);
ll ans=0;
for(int i=1;i<=2*n;i++)
{
scanf("%i %i",&x,&y);
if(x<1) ans+=1-x,x=1;
if(x>n) ans+=x-n,x=n;
if(y<1) ans+=1-y,y=1;
if(y>2) ans+=y-2,y=2;
cnt[x][y-1]++;
}
for(int i=n;i>=1;i--)
{
for(int k=1;k<=cnt[i][0];k++) pos[0][++sz[0]]=i;
for(int k=1;k<=cnt[i][1];k++) pos[1][++sz[1]]=i;
}
Clear(dp);
if(sz[0]) dp[sz[0]-1]=pos[0][sz[0]]-1;
if(sz[1]) dp[sz[0]]=pos[1][sz[1]];
for(int i=2,l=1;i<=2*n;i++)
{
Clear(tmp);
int f=0;
if(i%2==0) f=1;
for(int j=0;j<=sz[0];j++) if(dp[j]!=inf)
{
int k=2*n-i-j+1;
if(j) ckmn(tmp[j-1],dp[j]+abs(pos[0][j]-l)+f);
if(k) ckmn(tmp[j],dp[j]+abs(pos[1][k]-l)+(f^1));
}
for(int j=0;j<=sz[0];j++) dp[j]=tmp[j];
if(i%2==0) l++;
}
ans+=dp[0];
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |