답안 #889006

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
889006 2023-12-18T15:15:10 Z nnhzzz Simfonija (COCI19_simfonija) C++14
110 / 110
24 ms 12772 KB
/* [Author: Nguyen Ngoc Hung] - From THPT Ngo Gia Tu with Love */

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
#include <chrono>

using namespace std;

#define        __nnhzzz__  signed main()
#define          BIT(i,j)  (((i)>>(j))&1LL)
#define           MASK(i)  (1LL<<(i))
#define            ALL(x)  (x).begin(),(x).end()
#define             SZ(x)  (int)(x).size()
#define                fi  first
#define                se  second
#define                ll  long long
#define                ld  long double
#define                vi  vector<int>
#define               vvi  vector<vi>
#define              vvvi  vector<vvi>
#define               pii  pair<int,int>
#define              vpii  vector<pii>
#define             vvpii  vector<vpii>
#define REPDIS(i,be,en,j)  for(int i = (be); i<=(en); i+=j)
#define     REPD(i,be,en)  for(int i = (be); i>=(en); i--)
#define      REP(i,be,en)  for(int i = (be); i<=(en); i++)
#define              endl  "\n"
#define                MP  make_pair
#define               int  ll

//-----------------------------------------------------------------------------------------------//
int readInt(){
    char c;
    do{ c = getchar(); }while(c!='-' && !isdigit(c));
    bool neg = (c=='-');
    int res = neg?0:c-'0';
    while(isdigit(c=getchar())) res = (res<<3)+(res<<1)+(c-'0');
    return neg?-res:res;
}
//------------------------------------------------------------------------------------------------//
const  ll   LINF = 1e18;
const int    INF = 1e9;
const int    LOG = 20;
const int   MAXN = 5e5+7;
const int      N = 1e2+3;
const int    MOD = 1e9+7;
const int   BASE = 1e5;
const  ld    EPS = 1e-9;
const  ld     PI = acos(-1);
const int OFFSET = 1e3;
//------------------------------------------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
//------------------------------------------------------------------------------------------------//

/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Nguyen Ngoc Hung - nnhzzz
    Training for VOI24 gold medal
----------------------------------------------------------------
*/

int a[MAXN],b[MAXN];
int n,k;

namespace sub1{
	int c[MAXN];

	int calc(int x){
		int res = 0;
		REP(i,1,n) res += abs(a[i]-(b[i]+x));
		return res;
	}

	void solve(){
		REP(i,1,n) c[i] = a[i]-b[i];
		sort(c+1,c+n+1);
		int res = min(calc(c[n/2]),calc(c[(n+1)/2]));
		cout << res;
	}
}

namespace sub2{
	int pre[MAXN];

	void solve(){
		REP(i,1,n) a[i] = b[i]-a[i];
		sort(a+1,a+n+1);
		REP(i,1,n) pre[i] = pre[i-1]+a[i];
		int l = 0,r = n-k,res = LINF;
		REP(i,1,n){
			while(l<i && r<n && a[r+1]-a[i]<a[i]-a[l+1]) ++l,++r;
			mini(res,(pre[r]-pre[i])-1LL*a[i]*(r-i)+a[i]*(i-l)-(pre[i]-pre[l]));
		}
		cout << res;
	}
}

void solve(){
	cin >> n >> k;
	REP(i,1,n) cin >> a[i];
	REP(i,1,n) cin >> b[i];
	if(k==0){
		sub1::solve();
		return ;
	}
	sub2::solve();
}

__nnhzzz__{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define task "test"
    if(fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    #define task1 "nnhzzz"
    if(fopen(task1".inp","r")){
        freopen(task1".inp","r",stdin);
        freopen(task1".out","w",stdout);
    }

    int test = 1;

    while(test--){
      solve();
    }
    cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
    return 0;
}

/**  /\_/\
 *  (= ._.)
 *  / >TL \>AC
**/

Compilation message

simfonija.cpp: In function 'int main()':
simfonija.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
simfonija.cpp:154:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
simfonija.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |         freopen(task1".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
simfonija.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |         freopen(task1".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 12636 KB Output is correct
2 Correct 19 ms 12764 KB Output is correct
3 Correct 22 ms 12508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 12636 KB Output is correct
2 Correct 19 ms 12632 KB Output is correct
3 Correct 17 ms 12772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 12636 KB Output is correct
2 Correct 18 ms 12632 KB Output is correct
3 Correct 19 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 10840 KB Output is correct
2 Correct 17 ms 10844 KB Output is correct
3 Correct 18 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 10840 KB Output is correct
2 Correct 21 ms 12128 KB Output is correct
3 Correct 22 ms 12104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 10840 KB Output is correct
2 Correct 18 ms 10720 KB Output is correct
3 Correct 21 ms 12112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 10832 KB Output is correct
2 Correct 24 ms 12368 KB Output is correct
3 Correct 23 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 10840 KB Output is correct
2 Correct 22 ms 12124 KB Output is correct
3 Correct 21 ms 12144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 10840 KB Output is correct
2 Correct 18 ms 12120 KB Output is correct
3 Correct 22 ms 12132 KB Output is correct