結果

問題 No.738 平らな農地
ユーザー 👑 kmjpkmjp
提出日時 2018-09-28 22:36:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,733 bytes
コンパイル時間 1,594 ms
コンパイル使用メモリ 164,488 KB
実行使用メモリ 22,824 KB
最終ジャッジ日時 2024-04-20 11:16:12
合計ジャッジ時間 7,533 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
17,636 KB
testcase_01 AC 4 ms
17,628 KB
testcase_02 AC 4 ms
17,764 KB
testcase_03 AC 5 ms
17,632 KB
testcase_04 AC 4 ms
17,756 KB
testcase_05 AC 6 ms
17,800 KB
testcase_06 AC 7 ms
17,808 KB
testcase_07 AC 6 ms
17,808 KB
testcase_08 AC 5 ms
17,780 KB
testcase_09 AC 5 ms
17,644 KB
testcase_10 AC 5 ms
17,768 KB
testcase_11 AC 6 ms
17,784 KB
testcase_12 AC 5 ms
17,664 KB
testcase_13 AC 6 ms
17,816 KB
testcase_14 AC 5 ms
17,772 KB
testcase_15 AC 56 ms
19,948 KB
testcase_16 AC 60 ms
19,432 KB
testcase_17 AC 64 ms
19,688 KB
testcase_18 AC 63 ms
19,728 KB
testcase_19 AC 74 ms
20,556 KB
testcase_20 AC 58 ms
20,452 KB
testcase_21 AC 70 ms
19,984 KB
testcase_22 AC 59 ms
19,716 KB
testcase_23 AC 68 ms
19,664 KB
testcase_24 AC 70 ms
19,652 KB
testcase_25 AC 6 ms
17,776 KB
testcase_26 AC 5 ms
17,912 KB
testcase_27 AC 5 ms
17,776 KB
testcase_28 AC 5 ms
17,772 KB
testcase_29 AC 5 ms
17,780 KB
testcase_30 AC 5 ms
17,780 KB
testcase_31 AC 5 ms
17,776 KB
testcase_32 AC 5 ms
17,776 KB
testcase_33 AC 6 ms
17,776 KB
testcase_34 AC 5 ms
17,784 KB
testcase_35 AC 5 ms
19,828 KB
testcase_36 AC 6 ms
17,788 KB
testcase_37 AC 5 ms
17,776 KB
testcase_38 AC 5 ms
17,668 KB
testcase_39 AC 5 ms
17,788 KB
testcase_40 AC 6 ms
17,904 KB
testcase_41 AC 5 ms
17,776 KB
testcase_42 AC 5 ms
17,772 KB
testcase_43 AC 5 ms
17,776 KB
testcase_44 AC 5 ms
17,772 KB
testcase_45 AC 59 ms
20,568 KB
testcase_46 AC 57 ms
19,584 KB
testcase_47 AC 57 ms
21,144 KB
testcase_48 AC 53 ms
19,856 KB
testcase_49 AC 52 ms
20,568 KB
testcase_50 AC 52 ms
19,904 KB
testcase_51 AC 58 ms
19,892 KB
testcase_52 AC 56 ms
19,520 KB
testcase_53 AC 56 ms
19,788 KB
testcase_54 AC 61 ms
19,672 KB
testcase_55 AC 60 ms
19,568 KB
testcase_56 AC 60 ms
19,948 KB
testcase_57 AC 54 ms
19,840 KB
testcase_58 AC 56 ms
19,520 KB
testcase_59 AC 57 ms
20,248 KB
testcase_60 AC 54 ms
21,480 KB
testcase_61 AC 57 ms
20,560 KB
testcase_62 AC 53 ms
19,572 KB
testcase_63 AC 62 ms
19,836 KB
testcase_64 AC 59 ms
20,312 KB
testcase_65 AC 74 ms
21,108 KB
testcase_66 AC 75 ms
19,472 KB
testcase_67 AC 47 ms
19,304 KB
testcase_68 AC 47 ms
19,732 KB
testcase_69 AC 58 ms
19,752 KB
testcase_70 AC 51 ms
22,824 KB
testcase_71 AC 53 ms
19,832 KB
testcase_72 AC 55 ms
19,984 KB
testcase_73 AC 50 ms
19,644 KB
testcase_74 AC 57 ms
20,668 KB
testcase_75 AC 62 ms
19,508 KB
testcase_76 AC 59 ms
21,084 KB
testcase_77 AC 65 ms
19,496 KB
testcase_78 AC 68 ms
19,892 KB
testcase_79 AC 63 ms
19,640 KB
testcase_80 AC 56 ms
20,316 KB
testcase_81 AC 60 ms
20,264 KB
testcase_82 AC 64 ms
21,076 KB
testcase_83 AC 55 ms
20,464 KB
testcase_84 AC 61 ms
20,028 KB
testcase_85 AC 80 ms
19,840 KB
testcase_86 AC 74 ms
19,612 KB
testcase_87 AC 37 ms
19,700 KB
testcase_88 AC 37 ms
19,780 KB
testcase_89 AC 5 ms
17,756 KB
testcase_90 AC 5 ms
17,760 KB
testcase_91 AC 4 ms
17,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<class V, int ME> class BIT {
public:
	V bit[1<<ME],val[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
	void set(int e,V v) { add(e,v-val[e]);}
	int lower_bound(V val) {
		V tv=0; int i,ent=0;
		for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
		return ent;
	}
};
BIT<ll,20> num,tot;

int N,K;
int A[101010];
int R[101010];
pair<ll,int> P[101010];

ll hoge() {
	int p=(K+1)/2;
	int x=num.lower_bound(p);
	
	ll ret=0;
	ret+=(p-1)*P[x].first-tot(x-1);
	ret+=(tot(N)-tot(x))-(K-p)*P[x].first;
	return ret;
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N) {
		cin>>A[i];
		P[i]={A[i],i};
	}
	sort(P,P+N);
	FOR(i,N) R[P[i].second]=i;
	
	ll mi=1LL<<60;
	FOR(i,K-1) {
		num.add(R[i],1);
		tot.add(R[i],A[i]);
	}
	for(i=0;i+K<=N;i++) {
		num.add(R[i+K-1],1);
		tot.add(R[i+K-1],A[i+K-1]);
		mi=min(mi,hoge());
		num.add(R[i],-1);
		tot.add(R[i],-A[i]);
	}
	cout<<mi<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0