結果

問題 No.738 平らな農地
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2019-10-08 21:34:24
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 7,292 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 63,920 KB
実行使用メモリ 42,304 KB
最終ジャッジ日時 2023-08-07 13:54:30
合計ジャッジ時間 20,270 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
22,844 KB
testcase_01 AC 60 ms
20,876 KB
testcase_02 AC 59 ms
20,976 KB
testcase_03 AC 60 ms
20,760 KB
testcase_04 AC 61 ms
22,820 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 AC 113 ms
30,116 KB
testcase_66 AC 113 ms
30,368 KB
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 AC 164 ms
32,348 KB
testcase_72 AC 183 ms
36,000 KB
testcase_73 AC 177 ms
38,316 KB
testcase_74 AC 184 ms
35,192 KB
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 WA -
testcase_84 WA -
testcase_85 AC 130 ms
37,852 KB
testcase_86 AC 124 ms
32,948 KB
testcase_87 WA -
testcase_88 WA -
testcase_89 AC 59 ms
22,876 KB
testcase_90 AC 59 ms
20,840 KB
testcase_91 AC 60 ms
18,740 KB
権限があれば一括ダウンロードができます

ソースコード

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;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var a=sc.Ia;
		var avl=new Avl();
		long m=0;
		for(int i = 0;i<s[1];i++) {
			avl.Ud(a[i],a[i]);
			m+=a[i];
		}
		int c=avl.Ra((avl.cnt+1)>>1).n;
		long ans=avl.Rd(c+1,mod)-avl.Rg(c+1,mod)*c+avl.Rg(0,c-1)*c-avl.Rd(0,c-1);
		for(int i = s[1];i<s[0];i++) {
			avl.Dl(a[i-s[1]]);
			m-=a[i-s[1]];
			avl.Ud(a[i],a[i]);
			m+=a[i];
			c=avl.Ra((avl.cnt+1)>>1).n;
			ans=Min(ans,avl.Rd(c+1,mod)-avl.Rg(c+1,mod)*c+avl.Rg(0,c-1)*c-avl.Rd(0,c-1));
		}
		Console.WriteLine("{0}",ans);
	}
}
public class Avl{
	public class Nd{
		public int h=1,c=1;
		public readonly int n;
		public int d,ds;
		public Nd l,r;
		public Nd(int n,int d,Nd l,Nd r){this.n=n;this.d=d;ds=d;this.l=l;this.r=r;}
	}
	public Nd root,du;
	public int cnt=0;
	public Avl(){
		du=new Nd(int.MinValue,0,du,du);du.h=0;du.c=0;
		root=du;
	}
	public void Ud(int n,int d){
		Nd mn=new Nd(n,d,du,du);
		if(root==du){root=mn;}
		else{
			Fu(root,n,mn);
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		}
		cnt++;
	}
	private void Fu(Nd t,int n,Nd mn){
		t.c++;
		t.ds+=mn.d;
		if(t.n>n){
			if(t.l!=du){
				Fu(t.l,n,mn);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
			}
			else{t.l=mn;}
		}
		else{
			if(t.r!=du){
				Fu(t.r,n,mn);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
			}
			else{t.r=mn;}
		}
		t.h=Max(t.l.h,t.r.h)+1;
	}
	private Nd Rotate(Nd t){
		Nd nd=du;
		if(t.l.h>t.r.h){
			if(t.l.l.h>t.l.r.h){
				nd=t.l;t.l=t.l.r;nd.r=t;
				Ft(t);
			}
			else{
				nd=t.l.r;
				t.l.r=nd.l;nd.l=t.l;
				Ft(t.l);
				t.l=nd.r;nd.r=t;
				Ft(t);
			}
		}
		else{
			if(t.r.l.h>t.r.r.h){
				nd=t.r.l;
				t.r.l=nd.r;nd.r=t.r;
				Ft(t.r);
				t.r=nd.l;nd.l=t;
				Ft(t);
			}
			else{
				nd=t.r;t.r=t.r.l;nd.l=t;
				Ft(t);
			}
		}
		Ft(nd);
		return nd;
	}
	private void Ft(Nd t){
		t.h=Max(t.l.h,t.r.h)+1;
		t.c=t.l.c+t.r.c+1;
		t.ds=t.l.ds+t.r.ds+t.d;
	}
	public bool Dl(int n){
		Nd t=root;
		if(t.n==n){
			root=Fd2(root);
			if(cnt==0){return true;}
			else if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			else{Ft(root);}
			return true;
		}
		bool bo=Fd1(root,n);
		if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		else{Ft(root);}
		return bo;
	}
	private bool Fd1(Nd t,int n){
		if(t.n>n){
			if(t.l==du){return false;}
			else if(t.l.n!=n){
				bool bo=Fd1(t.l,n);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
				else{Ft(t.l);}
				return bo;
			}
			else{
				t.l=Fd2(t.l);
				Ft(t);
				return true;
			}
		}
		else{
			if(t.r==du){return false;}
			else if(t.r.n!=n){
				bool bo=Fd1(t.r,n);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
				else{Ft(t.r);}
				return bo;
			}
			else{
				t.r=Fd2(t.r);
				Ft(t);
				return true;
			}
		}
	}
	private Nd Fd2(Nd dn){
		cnt--;
		if(dn.l==du){return dn.r;}
		if(dn.r==du){return dn.l;}
		Nd u=dn.l;
		if(u.r==du){
			u.r=dn.r;
			Ft(u);
			return u;
		}
		u=Fd3(u,dn);
		if(Abs(dn.l.l.h-dn.l.r.h)>1){u.l=Rotate(dn.l);}
		else{Ft(dn.l);}
		Ft(u);
		return u;
	}
	private Nd Fd3(Nd u,Nd dn){
		if(u.r.r!=du){
			Nd v=Fd3(u.r,dn);
			if(Abs(u.r.l.h-u.r.r.h)>1){u.r=Rotate(u.r);}
			else{Ft(u.r);}
			return v;
		}
		else{
			Nd v=u.r;u.r=u.r.l;v.l=dn.l;v.r=dn.r;
			return v;
		}
	}
	public Nd Hs(int n){
		Nd t=root;
		if(cnt==0){return null;}
		if(t.n==n){return t;}
		Nd a=null;
		while(true){
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{a=t.l;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{a=t.r;break;}
			}
		}
		return a;
	}
	public Nd Ra(int n){return cnt>=n?Fr(root,n):null;}
	private Nd Fr(Nd t,int n){
		if(t.l.c>n-1){return Fr(t.l,n);}
		if(t.l.c<n-1){return Fr(t.r,n-t.l.c-1);}
		return t;
	}
	public Nd Lb(int n){return cnt!=0?Flb(root,n):null;}
	private Nd Flb(Nd t,int n){
		if(t.n<n){
			if(t.r==du){return null;}
			return Flb(t.r,n);
		}
		if(t.n>n){
			if(t.l==du){return t;}
			Nd u=Flb(t.l,n);
			return u==null?t:u;
		}
		return t;
	}
	public Nd Ub(int n){return cnt!=0?Fub(root,n):null;}
	private Nd Fub(Nd t,int n){
		if(t.n>n){
			if(t.l==du){return null;}
			return Fub(t.l,n);
		}
		if(t.n<n){
			if(t.r==du){return t;}
			Nd u=Fub(t.r,n);
			return u==null?t:u;
		}
		return t;
	}
	public int Rg(int a,int b){return cnt!=0?Frg(root,a,b,false,false):0;}
	private int Frg(Nd t,int a,int b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.c;}
		if(t.n<a){return Frg(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frg(t.l,a,b,l,t.n<=b);}
		return Frg(t.l,a,b,l,t.n<=b)+Frg(t.r,a,b,t.n>=a,r)+1;
	}
	public int Rd(int a,int b){return cnt!=0?Frd(root,a,b,false,false):0;}
	private int Frd(Nd t,int a,int b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.ds;}
		if(t.n<a){return Frd(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frd(t.l,a,b,l,t.n<=b);}
		return Frd(t.l,a,b,l,t.n<=b)+Frd(t.r,a,b,t.n>=a,r)+t.d;
	}
	public void Ad(int n,int d){
		Nd t=root;
		if(cnt==0){return;}
		if(t.n==n){t.d+=d;t.ds+=d;return;}
		while(true){
			t.ds+=d;
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{t.l.d+=d;t.l.ds+=d;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{t.r.d+=d;t.r.ds+=d;break;}
			}
		}
	}
}

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 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