Submission #145272

#TimeUsernameProblemLanguageResultExecution timeMemory
145272TadijaSebezCoin Collecting (JOI19_ho_t4)C++11
37 / 100
49 ms3576 KiB
#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)

joi2019_ho_t4.cpp: In function 'int main()':
joi2019_ho_t4.cpp:13:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
joi2019_ho_t4.cpp:17:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...