結果

問題 No.738 平らな農地
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2020-04-12 09:07:40
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 639 ms / 2,000 ms
コード長 6,308 bytes
コンパイル時間 5,710 ms
コンパイル使用メモリ 107,652 KB
実行使用メモリ 78,064 KB
最終ジャッジ日時 2023-10-22 00:53:18
合計ジャッジ時間 27,814 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
18,312 KB
testcase_01 AC 27 ms
18,312 KB
testcase_02 AC 27 ms
18,312 KB
testcase_03 AC 26 ms
18,312 KB
testcase_04 AC 26 ms
18,312 KB
testcase_05 AC 39 ms
19,900 KB
testcase_06 AC 40 ms
19,912 KB
testcase_07 AC 39 ms
19,912 KB
testcase_08 AC 34 ms
19,596 KB
testcase_09 AC 32 ms
19,100 KB
testcase_10 AC 31 ms
18,924 KB
testcase_11 AC 34 ms
19,624 KB
testcase_12 AC 33 ms
19,176 KB
testcase_13 AC 39 ms
19,888 KB
testcase_14 AC 33 ms
19,240 KB
testcase_15 AC 274 ms
72,448 KB
testcase_16 AC 278 ms
73,464 KB
testcase_17 AC 309 ms
73,024 KB
testcase_18 AC 298 ms
68,100 KB
testcase_19 AC 343 ms
76,148 KB
testcase_20 AC 280 ms
73,480 KB
testcase_21 AC 339 ms
76,320 KB
testcase_22 AC 286 ms
73,644 KB
testcase_23 AC 326 ms
74,556 KB
testcase_24 AC 331 ms
74,024 KB
testcase_25 AC 33 ms
19,328 KB
testcase_26 AC 33 ms
19,324 KB
testcase_27 AC 33 ms
19,328 KB
testcase_28 AC 33 ms
19,320 KB
testcase_29 AC 34 ms
19,336 KB
testcase_30 AC 34 ms
19,312 KB
testcase_31 AC 33 ms
19,312 KB
testcase_32 AC 34 ms
19,324 KB
testcase_33 AC 34 ms
19,316 KB
testcase_34 AC 33 ms
19,320 KB
testcase_35 AC 33 ms
19,308 KB
testcase_36 AC 33 ms
19,320 KB
testcase_37 AC 33 ms
19,320 KB
testcase_38 AC 33 ms
19,304 KB
testcase_39 AC 34 ms
19,084 KB
testcase_40 AC 34 ms
19,312 KB
testcase_41 AC 33 ms
19,308 KB
testcase_42 AC 33 ms
19,304 KB
testcase_43 AC 33 ms
19,312 KB
testcase_44 AC 33 ms
19,312 KB
testcase_45 AC 278 ms
77,676 KB
testcase_46 AC 267 ms
72,384 KB
testcase_47 AC 273 ms
75,892 KB
testcase_48 AC 249 ms
73,564 KB
testcase_49 AC 245 ms
72,984 KB
testcase_50 AC 254 ms
75,488 KB
testcase_51 AC 283 ms
76,076 KB
testcase_52 AC 258 ms
72,984 KB
testcase_53 AC 266 ms
74,164 KB
testcase_54 AC 279 ms
76,476 KB
testcase_55 AC 280 ms
71,800 KB
testcase_56 AC 274 ms
75,184 KB
testcase_57 AC 254 ms
72,216 KB
testcase_58 AC 263 ms
74,756 KB
testcase_59 AC 268 ms
77,680 KB
testcase_60 AC 262 ms
77,088 KB
testcase_61 AC 265 ms
76,640 KB
testcase_62 AC 252 ms
72,400 KB
testcase_63 AC 288 ms
77,676 KB
testcase_64 AC 276 ms
76,640 KB
testcase_65 AC 529 ms
73,456 KB
testcase_66 AC 555 ms
75,144 KB
testcase_67 AC 395 ms
73,216 KB
testcase_68 AC 391 ms
75,056 KB
testcase_69 AC 560 ms
76,048 KB
testcase_70 AC 464 ms
78,020 KB
testcase_71 AC 354 ms
74,088 KB
testcase_72 AC 323 ms
74,396 KB
testcase_73 AC 314 ms
74,460 KB
testcase_74 AC 370 ms
76,040 KB
testcase_75 AC 528 ms
73,004 KB
testcase_76 AC 395 ms
75,048 KB
testcase_77 AC 597 ms
73,000 KB
testcase_78 AC 639 ms
77,840 KB
testcase_79 AC 621 ms
78,048 KB
testcase_80 AC 479 ms
75,660 KB
testcase_81 AC 543 ms
73,796 KB
testcase_82 AC 483 ms
76,248 KB
testcase_83 AC 353 ms
78,064 KB
testcase_84 AC 313 ms
76,180 KB
testcase_85 AC 567 ms
77,436 KB
testcase_86 AC 520 ms
72,832 KB
testcase_87 AC 192 ms
75,148 KB
testcase_88 AC 183 ms
72,976 KB
testcase_89 AC 26 ms
18,308 KB
testcase_90 AC 26 ms
18,308 KB
testcase_91 AC 26 ms
18,308 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var a=sc.Ia;
		var wm=new Wm(a);
		long ans=long.MaxValue;
		for(int i = 0;i<=s[0]-s[1];i++) {
			long c=wm.Qt(i,i+s[1]-1,(s[1]+1)>>1);
			ans=Min(ans,c*wm.Rl(i,i+s[1]-1,c)-wm.Rls(i,i+s[1]-1,c)+wm.Rms(i,i+s[1]-1,c)-c*wm.Rm(i,i+s[1]-1,c));
		}
		Console.WriteLine(ans);
	}
}
public class Wm{
	private int[][] ra;
	private long[][] fa;
	private int[] za;
	private const int b=30;
	private int n;
	public Wm(int[] a){
		n=a.Length;
		var d=new int[b+1][];
		ra=new int[b+1][];
		fa=new long[b+2][];
		for(int i = 0;i<=b;i++) {
			d[i]=new int[n];
			ra[i]=new int[n+1];
			fa[i]=new long[n+1];
		}
		fa[b+1]=new long[n+1];
		var d0=new int[2][]{new int[n],new int[n]};
		var d1=new int[2][]{new int[n],new int[n]};
		var h0=new int[]{0,0};
		var h1=new int[]{0,0};
		za=new int[b+1];
		for(int i = 0;i<n;i++) {
			if((a[i]&(1<<b))==0){
				d0[1][h0[1]]=a[i];
				h0[1]++;
				za[b]++;
			}
			else{
				d1[1][h1[1]]=a[i];
				h1[1]++;
				d[b][i]=ra[b][i+1]=1;
			}
			ra[b][i+1]+=ra[b][i];
			fa[b+1][i+1]+=fa[b+1][i]+a[i];
		}
		for(int i = 1;i<=b;i++) {
			int g=(1<<(b-i)),p=i&1;
			h0[p^1]=h1[p^1]=0;
			for(int j = 0;j<h0[p];j++) {
				if((d0[p][j]&g)==0){
					d0[p^1][h0[p^1]]=d0[p][j];
					h0[p^1]++;
					za[b-i]++;
				}
				else{
					d1[p^1][h1[p^1]]=d0[p][j];
					h1[p^1]++;
					d[b-i][j]=ra[b-i][j+1]=1;
				}
				ra[b-i][j+1]+=ra[b-i][j];
				fa[b-i+1][j+1]+=fa[b-i+1][j]+d0[p][j];
			}
			for(int j = 0;j<h1[p];j++) {
				if((d1[p][j]&g)==0){
					d0[p^1][h0[p^1]]=d1[p][j];
					h0[p^1]++;
					za[b-i]++;
				}
				else{
					d1[p^1][h1[p^1]]=d1[p][j];
					h1[p^1]++;
					d[b-i][h0[p]+j]=ra[b-i][h0[p]+j+1]=1;
				}
				ra[b-i][h0[p]+j+1]+=ra[b-i][h0[p]+j];
				fa[b-i+1][h0[p]+j+1]+=fa[b-i+1][h0[p]+j]+d1[p][j];
			}
		}
		int v=(b&1)^1;
		for(int j = 0;j<h0[v];j++) {fa[0][j+1]+=fa[0][j]+d0[v][j];}
		for(int j = 0;j<h1[v];j++) {fa[0][h0[v]+j+1]+=fa[0][h0[v]+j]+d1[v][j];}
	}
	public int Rk(int l,int r,long k){
		l--;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return r-l;
	}
	public int Rl(int l,int r,long k){
		l--;
		int p=0;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				p+=r-l;
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
				p-=r-l;
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return p;
	}
	public int Rm(int l,int r,long k){
		l--;
		int p=0;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
			}
			else{
				p+=r-l;
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
				p-=r-l;
			}
		}
		return p;
	}
	public long Rls(int l,int r,long k){
		l--;
		long p=0;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				p+=fa[i+1][r+1]-fa[i+1][l+1];
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
				p-=fa[i][r+1]-fa[i][l+1];
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return p;
	}
	public long Rms(int l,int r,long k){
		l--;
		long p=0;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
			}
			else{
				p+=fa[i+1][r+1]-fa[i+1][l+1];
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
				p-=fa[i][r+1]-fa[i][l+1];
			}
		}
		return p;
	}
	public int Qt(int l,int r,int k){
		l--;
		int p=0;
		for(int i = b;i>=0;i--) {
			if((r-ra[i][r+1])-(l-ra[i][l+1])<k){
				k-=(r-ra[i][r+1])-(l-ra[i][l+1]);
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
				p|=1<<i;
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return p;
	}
	public int Pv(int l,int r,int k){
		int p=Rl(l,r,k);
		return p!=0?Qt(l,r,p):-1;
	}
	public int Nv(int l,int r,int k){
		int p=Rm(l,r,k);
		return p!=0?Qt(l,r,r-l+1-p+1):-1;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}
0