結果
| 問題 | No.738 平らな農地 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-20 15:29:53 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 66 ms / 2,000 ms |
| コード長 | 1,873 bytes |
| 記録 | |
| コンパイル時間 | 2,277 ms |
| コンパイル使用メモリ | 218,544 KB |
| 実行使用メモリ | 8,308 KB |
| 最終ジャッジ日時 | 2026-02-20 15:30:02 |
| 合計ジャッジ時間 | 8,456 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 87 |
ソースコード
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define fi first
#define sc second
#define pii pair<int,int>
#define pdd pair<double,double>
#define pb push_back
#define umap unordered_map
#define mset multiset
#define pq priority_queue
#define ull unsigned long long
#define i128 __int128
#define ld long double
#define fixs fixed<<setprecision
#define FileIn(x) freopen(x".in","r",stdin)
#define FileOut(x) freopen(x".out","w",stdout)
#define FileIO(x) FileIn(x),FileOut(x);
const int maxn=1e5+10;
int lb(int x){
return x&(-x);
}
struct BIT{
int c[maxn],N;
void init(int n){
N=n;
for(int i=1;i<=n;i++) c[i]=0;
}
void upd(int x,int k){
for(;x<=N;x+=lb(x)) c[x]+=k;
}
int query(int x){
int sum=0;
for(;x;x-=lb(x)) sum+=c[x];
return sum;
}
}tr,trs;
int n,res,a[maxn],k,w[maxn],num[maxn],cnt,t[maxn];
void solve(){
cin>>n>>k,tr.init(n),trs.init(n),res=1e18;
for(int i=1;i<=n;i++) cin>>a[i],num[i]=a[i];
sort(num+1,num+n+1),cnt=unique(num+1,num+n+1)-num-1;
for(int i=1;i<=n;i++) w[i]=lower_bound(num+1,num+cnt+1,a[i])-num,t[w[i]]=a[i];
for(int i=1;i<k;i++) tr.upd(w[i],1),trs.upd(w[i],a[i]);
for(int i=k;i<=n;i++){
tr.upd(w[i],1),trs.upd(w[i],a[i]);
int l=1,r=n,p=0,mid=0;
while(l<=r) mid=(l+r)>>1,(tr.query(mid)>=k/2+(k&1)?p=mid,r=mid-1:l=mid+1);
int x=t[p]*tr.query(p)-trs.query(p),y=(trs.query(n)-trs.query(p))-t[p]*(tr.query(n)-tr.query(p));
res=min(res,x+y),tr.upd(w[i-k+1],-1),trs.upd(w[i-k+1],-a[i-k+1]);
}
cout<<res<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}
/*
Samples
input:
output:
THINGS TODO:
检查freopen,尤其是后缀名
检查空间
检查调试语句是否全部注释
*/