結果

問題 No.738 平らな農地
ユーザー steinumsteinum
提出日時 2020-12-11 19:25:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 2,994 bytes
コンパイル時間 2,592 ms
コンパイル使用メモリ 209,596 KB
実行使用メモリ 32,156 KB
最終ジャッジ日時 2023-10-20 01:37:36
合計ジャッジ時間 12,028 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,724 KB
testcase_01 AC 2 ms
5,724 KB
testcase_02 AC 2 ms
5,716 KB
testcase_03 AC 2 ms
5,716 KB
testcase_04 AC 2 ms
5,716 KB
testcase_05 AC 6 ms
10,284 KB
testcase_06 AC 6 ms
10,340 KB
testcase_07 AC 5 ms
10,356 KB
testcase_08 AC 4 ms
8,004 KB
testcase_09 AC 3 ms
7,884 KB
testcase_10 AC 3 ms
7,844 KB
testcase_11 AC 4 ms
8,020 KB
testcase_12 AC 3 ms
7,908 KB
testcase_13 AC 5 ms
10,244 KB
testcase_14 AC 3 ms
7,920 KB
testcase_15 AC 84 ms
30,440 KB
testcase_16 AC 84 ms
30,720 KB
testcase_17 AC 89 ms
30,612 KB
testcase_18 AC 87 ms
30,256 KB
testcase_19 AC 101 ms
31,824 KB
testcase_20 AC 86 ms
30,744 KB
testcase_21 AC 97 ms
31,900 KB
testcase_22 AC 88 ms
30,848 KB
testcase_23 AC 93 ms
31,260 KB
testcase_24 AC 92 ms
30,892 KB
testcase_25 AC 4 ms
7,948 KB
testcase_26 AC 3 ms
7,948 KB
testcase_27 AC 4 ms
7,948 KB
testcase_28 AC 3 ms
7,940 KB
testcase_29 AC 3 ms
7,952 KB
testcase_30 AC 4 ms
7,940 KB
testcase_31 AC 4 ms
7,940 KB
testcase_32 AC 4 ms
7,948 KB
testcase_33 AC 3 ms
7,940 KB
testcase_34 AC 3 ms
7,936 KB
testcase_35 AC 4 ms
7,940 KB
testcase_36 AC 4 ms
7,936 KB
testcase_37 AC 4 ms
7,940 KB
testcase_38 AC 4 ms
7,940 KB
testcase_39 AC 4 ms
7,952 KB
testcase_40 AC 3 ms
7,940 KB
testcase_41 AC 3 ms
7,940 KB
testcase_42 AC 3 ms
7,936 KB
testcase_43 AC 4 ms
7,940 KB
testcase_44 AC 3 ms
7,940 KB
testcase_45 AC 93 ms
32,152 KB
testcase_46 AC 86 ms
30,424 KB
testcase_47 AC 89 ms
31,548 KB
testcase_48 AC 85 ms
30,828 KB
testcase_49 AC 84 ms
30,644 KB
testcase_50 AC 86 ms
31,484 KB
testcase_51 AC 91 ms
31,696 KB
testcase_52 AC 85 ms
30,656 KB
testcase_53 AC 89 ms
31,028 KB
testcase_54 AC 91 ms
31,736 KB
testcase_55 AC 92 ms
31,680 KB
testcase_56 AC 90 ms
31,368 KB
testcase_57 AC 85 ms
30,292 KB
testcase_58 AC 89 ms
31,260 KB
testcase_59 AC 91 ms
32,156 KB
testcase_60 AC 91 ms
32,020 KB
testcase_61 AC 90 ms
31,844 KB
testcase_62 AC 84 ms
30,432 KB
testcase_63 AC 94 ms
32,148 KB
testcase_64 AC 91 ms
31,828 KB
testcase_65 AC 143 ms
30,696 KB
testcase_66 AC 152 ms
31,460 KB
testcase_67 AC 81 ms
30,444 KB
testcase_68 AC 83 ms
31,020 KB
testcase_69 AC 97 ms
31,352 KB
testcase_70 AC 91 ms
32,076 KB
testcase_71 AC 77 ms
31,756 KB
testcase_72 AC 90 ms
30,372 KB
testcase_73 AC 84 ms
30,860 KB
testcase_74 AC 92 ms
31,332 KB
testcase_75 AC 98 ms
30,324 KB
testcase_76 AC 91 ms
31,052 KB
testcase_77 AC 104 ms
30,308 KB
testcase_78 AC 114 ms
31,960 KB
testcase_79 AC 115 ms
32,120 KB
testcase_80 AC 101 ms
31,256 KB
testcase_81 AC 104 ms
30,576 KB
testcase_82 AC 102 ms
31,408 KB
testcase_83 AC 85 ms
32,156 KB
testcase_84 AC 84 ms
31,536 KB
testcase_85 AC 152 ms
31,996 KB
testcase_86 AC 138 ms
30,308 KB
testcase_87 AC 83 ms
31,236 KB
testcase_88 AC 80 ms
30,324 KB
testcase_89 AC 2 ms
4,348 KB
testcase_90 AC 2 ms
5,700 KB
testcase_91 AC 2 ms
5,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define ll long long

const int Maxn = 1e5+5;
const int M_Log = 20;
int arr[Maxn], n;
ll cum[Maxn];

struct wavelet_tree {
	int s[Maxn], tree[M_Log][Maxn], L[M_Log][Maxn];
	long long ls[M_Log][Maxn], SL;
	void build(int l, int r, int d)
	{
		if (l == r)return;
		int mid = (l + r) >> 1, cnt = 0, lc = l, rc = mid + 1, ec = 0;
		for (int i = l; i <= r; i++) if (tree[d][i] < s[mid])cnt++; // **
		for (int i = l; i <= r; i++)
		{
			if ((tree[d][i] < s[mid]) || (tree[d][i] == s[mid] && ec < (mid - l + 1 - cnt))) // **
			{
				tree[d + 1][lc++] = tree[d][i];
				ls[d][i] = ls[d][i - 1] + tree[d][i];
				if (tree[d][i] == s[mid])ec++;
			}
			else
			{
				tree[d + 1][rc++] = tree[d][i];
				ls[d][i] = ls[d][i - 1];
			}
			L[d][i] = L[d][l - 1] + lc - l; //বামে এখন পর্যন্ত কয়টা গেছে
		}
		build(l, mid, d + 1);
		build(mid + 1, r, d + 1);
	}
	void init(int *arr, int n)
	{
		for (int i = 1; i <= n; i++)
		{
			tree[0][i] = arr[i];
			s[i] = tree[0][i];
		}
		sort(s + 1, s + n + 1); // **
		build(1, n, 0);
	}
	//kth smallest element in [x, y]
	// SL= sum of first kth smallest elemnt in [x,y]
	int kth(int l, int r, int d, int x, int y, int k)
	{	//1,n,0,x,y,k
		if (l == r) {
			SL += tree[d][l];
			return tree[d][l];
		}
		int cnt = L[d][y] - L[d][x - 1];
		int mid = (l + r) >> 1;
		if (cnt >= k)
		{
			int newx = L[d][x - 1] - L[d][l - 1];
			int newy = L[d][y] - L[d][l - 1];
			return kth(l, mid, d + 1, l + newx, l + newy - 1, k);
		}
		else
		{
			int newx = x - l - (L[d][x - 1] - L[d][l - 1]);
			int newy = y - l + 1 - (L[d][y] - L[d][l - 1]);
			SL += ls[d][y] - ls[d][x - 1];
			return kth(mid + 1, r, d + 1, mid + 1 + newx, mid + newy, k - cnt);
		}
	}
	//count of nos in [l, r] Less than or equal to k
	//SL= sum of element less than or equal to k in [l,r]
	int LTE(int l, int r, int d, int x, int y, int k)
	{	//1,n,0,x,y,k
		if (l == r) {
			if (l > y || l < x)return 0;
			SL += tree[d][l] * (tree[d][l] <= k); // **
			return tree[d][l] <= k; // **
		}
		int cnt = L[d][y] - L[d][x - 1], mid = (l + r) >> 1;
		if (s[mid] <= k) // **
		{
			int newx = x - l - (L[d][x - 1] - L[d][l - 1]);
			int newy = y - l + 1 - (L[d][y] - L[d][l - 1]);
			SL += ls[d][y] - ls[d][x - 1];
			return cnt + LTE(mid + 1, r, d + 1, mid + 1 + newx, mid + newy, k);
		}
		else
		{
			int newx = L[d][x - 1] - L[d][l - 1];
			int newy = L[d][y] - L[d][l - 1];
			return LTE(l, mid, d + 1, l + newx, l + newy - 1, k);
		}
	}
}wv;

int main(){
	int n; int k; cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>arr[i];
		cum[i]=cum[i-1]+arr[i];
	}
	wv.init(arr,n);
	ll mi=1e18;
	for(int i=1,j=k;j<=n;i++,j++){
		ll m=wv.kth(1,n,0,i,j,(k+1)/2);
		wv.SL=0;
		ll cc=wv.LTE(1,n,0,i,j,m);
		ll cb=k-cc;
		ll choto=wv.SL;
		ll boro=(cum[j]-cum[i-1])-choto;
		ll tot=(boro-cb*m)+(cc*m-choto);
		mi=min(mi,tot);
	}cout<<mi<<endl;
}
0