Submission #50620525


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,a,n) for (ll i=a;i<(ll)(n);i++)
#define per(i,a,n) for (ll i=n;i-->(ll)(a);)
ll read(){ll r;scanf("%lld",&r);return r;}
const ll INF=0x3f3f3f3f'3f3f3f3f;

int main(){
  auto chmin=[&](auto&a,auto b){a=min(a,b);};
  int n=read();
  int k=read();
  vector<ll> a;
  vector<int> at;
  rep(i,0,n) {
    int v=read();
    a.push_back(v);
    at.push_back(v);
  }
  rep(i,0,k) at.push_back(read());
  sort(begin(a),end(a));
  sort(begin(at),end(at));
  vector<pair<int,int> > tcnt;
  rep(i,0,size(at)) {
    if(i==0 or at[i] != at[i-1]) {
      tcnt.emplace_back(at[i],1);
    }else{
      tcnt.back().second++;
    }
  }
  vector<ll> todd;
  for(auto [v,c]:tcnt) if(c%2==1) todd.push_back(v);
  int m=todd.size();
  if(m > n or (n-m)%2==1){
    printf("-1\n");
    return 0;
  }
  vector dp(n+1,vector<ll>(m+1,INF)); // dp[匹配个数][钦定个数]
  dp[0][0]=0;
  rep(i,0,n) rep(j,0,m+1) if(dp[i][j] != INF){
    if(j<m)  chmin(dp[i+1][j+1],dp[i][j]+abs(a[i+0]-todd[j])); // |a[i]-todd[j]|
    if(i+1<n)chmin(dp[i+2][j  ],dp[i][j]+abs(a[i+1]-a[i+0] )); // |a[i]-p| + |a[i+1]-p|
  }
  printf("%lld\n",dp[n][m]==INF?-1:dp[n][m]);
  return 0;
}

Submission Info

Submission Time
Task D - Moving Pieces on Line
User cromarmot
Language C++ 20 (gcc 12.2)
Score 600
Code Size 1222 Byte
Status AC
Exec Time 109 ms
Memory 199152 KiB

Compile Error

Main.cpp: In function ‘ll read()’:
Main.cpp:7:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    7 | ll read(){ll r;scanf("%lld",&r);return r;}
      |                ~~~~~^~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 24
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt, s4.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, s1.txt, s2.txt, s3.txt, s4.txt
Case Name Status Exec Time Memory
01.txt AC 3 ms 8000 KiB
02.txt AC 37 ms 67300 KiB
03.txt AC 54 ms 96416 KiB
04.txt AC 10 ms 20420 KiB
05.txt AC 1 ms 4404 KiB
06.txt AC 8 ms 15680 KiB
07.txt AC 19 ms 36020 KiB
08.txt AC 21 ms 38336 KiB
09.txt AC 14 ms 28060 KiB
10.txt AC 8 ms 17912 KiB
11.txt AC 88 ms 158484 KiB
12.txt AC 97 ms 173420 KiB
13.txt AC 102 ms 182252 KiB
14.txt AC 1 ms 3892 KiB
15.txt AC 2 ms 3812 KiB
16.txt AC 1 ms 3792 KiB
17.txt AC 2 ms 4032 KiB
18.txt AC 102 ms 184084 KiB
19.txt AC 1 ms 3912 KiB
20.txt AC 109 ms 199152 KiB
s1.txt AC 1 ms 3836 KiB
s2.txt AC 1 ms 3912 KiB
s3.txt AC 1 ms 3728 KiB
s4.txt AC 1 ms 3800 KiB