結果

問題 No.1248 Kth Sum
ユーザー mfbgjsczmfbgjscz
提出日時 2020-08-09 00:09:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,226 bytes
コンパイル時間 2,690 ms
コンパイル使用メモリ 204,048 KB
実行使用メモリ 7,688 KB
最終ジャッジ日時 2024-04-10 08:33:37
合計ジャッジ時間 5,945 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 89 ms
7,552 KB
testcase_14 AC 90 ms
6,944 KB
testcase_15 AC 88 ms
7,584 KB
testcase_16 AC 98 ms
6,940 KB
testcase_17 AC 97 ms
6,944 KB
testcase_18 AC 94 ms
6,940 KB
testcase_19 AC 95 ms
6,940 KB
testcase_20 AC 93 ms
6,944 KB
testcase_21 AC 88 ms
6,944 KB
testcase_22 AC 88 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 87 ms
7,688 KB
testcase_25 AC 114 ms
7,552 KB
testcase_26 AC 100 ms
7,556 KB
testcase_27 AC 85 ms
6,944 KB
testcase_28 AC 86 ms
6,940 KB
testcase_29 AC 85 ms
6,940 KB
testcase_30 AC 103 ms
7,040 KB
testcase_31 AC 96 ms
6,944 KB
testcase_32 AC 94 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 103 ms
6,944 KB
testcase_35 AC 96 ms
6,944 KB
testcase_36 AC 93 ms
6,940 KB
testcase_37 AC 93 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
/*#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
*/#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")    
using namespace std;
/*namespace mp = boost::multiprecision;
using Bint = mp::cpp_int;
using Real = mp::number<mp::cpp_dec_float<1024>>;
*/#define lli long long int
#define uli unsigned long long int
#define INF 9999999999999999
#define rep(i,m,n) for(lli i = m;i < n;i++)
#define rrep(i,m,n) for(lli i=m-1;i>=n;i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(),N.end()),N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(),n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(50) << S << endl
#define Vec(K,L,N,S) vector<L> K(N,S)
#define DV(K,L,N,M,S) vector<vector<L>> K(N,vector<L>(M,S))
#define TV(K,L,N,M,R,S) vector<vector<vector<L>>> K(N,vector<vector<L>>(M,vector<L>(R,S)))
#define pint pair<lli,lli>
#define paf(L,R) pair<L,R>
#define mod 1000000007
#define MAX 10000010
#define ALL(a)  a.begin(),a.end()
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
int main() {
  long long N, K, sum = 0, num = INF;
  cin >> N >> K;
  vector<long long> A(N);
  vector<long long> CM(N, INF); //後ろからA[i]の累積min
  rep(i, 0, N) cin >> A[i];
	if(!(K - 1)) cout << A[0] << endl; //K=1なら何もしないのが最適
  else {
		CM[N - 1] = A[N - 1];
  	rrep(i, N-1, 0) CM[i] = min(CM[i + 1], A[i]);
  	priority_queue<long long> pque; //max{A[i+1,min(N,i+X)]}
 	 	rrep(i, N, K - 1) { //前から見て最初にK番目になるindex
    	long long X = (i + K - 2) / (K - 1); //前から見てi番目が最初にK番目になるとき,部分列の個数の最小値
			if(!(X - 1)) chmin(num, A[i]); //部分列の個数が1つならこの時点で確定
			else if(K * X <= N) {
				while(pque.size() - X + 1) {
					sum -= pque.top(); pque.pop();
				}
				if(CM[K * X - 1] <= pque.top()) chmin(num, A[i] + sum);
				else chmin(num, A[i] + sum + CM[K * X - 1] - pque.top());
			}
			pque.push(A[i]);
			sum += A[i];
  	}
		cout << num << endl;
	}
}
0