QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#469270#8574. Swirly SortPhantomThreshold#RE 28ms244728kbC++204.5kb2024-07-09 16:56:402024-07-09 16:56:40

Judging History

你现在查看的是最新测评结果

  • [2024-07-09 16:56:40]
  • 评测
  • 测评结果:RE
  • 用时:28ms
  • 内存:244728kb
  • [2024-07-09 16:56:40]
  • 提交

answer

#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ll long long
#define int long long
#define lowbit(x) ((x)&(-x))
using namespace std;

const int maxn = 510000;
const int maxd = 20;

int n;
int a[maxn];
int t[maxn],tp;

struct node
{
	int c,lc,rc;
	node(){ c=lc=rc=0; }
}seg[maxn*maxd]; int root[maxn],tot;
int newnode()
{
	seg[++tot]=node();
	return tot;
}
int loc;
int ins(int x,const int l,const int r)
{
	int y=newnode();
	seg[y]=seg[x];
	seg[y].c++;
	if(l!=r)
	{
		int mid=(l+r)>>1;
		if(loc<=mid) seg[y].lc=ins(seg[y].lc,l,mid);
		else seg[y].rc=ins(seg[y].rc,mid+1,r);
	}
	return y;
}
int queryk(const int x,const int y,const int l,const int r,int k)
{
	if(l==r) return l;
	int mid=(l+r)>>1;
	if( seg[seg[y].lc].c-seg[seg[x].lc].c>=k ) return queryk(seg[x].lc,seg[y].lc,l,mid,k);
	else 
	{
		k-=seg[seg[y].lc].c-seg[seg[x].lc].c;
		return queryk(seg[x].rc,seg[y].rc,mid+1,r,k);
	}
}
/*
struct node
{
	int n1,n2;
	priority_queue<int>qx;
	priority_queue<int, vector<int> ,greater<int> >qn;
	
}
vector<node>;
typedef __gnu_pbds::tree< int, __gnu_pbds::null_type,
				  less< int >, __gnu_pbds::rb_tree_tag,
				  __gnu_pbds::tree_order_statistics_node_update >
				  PB;
vector<PB>V;*/

int calc(int l,int r)
{
	int num=r-l+1;
	return queryk( root[l-1],root[r],1,n,(num+1)/2 );
//	return *V[i].find_by_order( (num+1)/2-1 );
}
pair<int,int>ai[maxn];

ll solve()
{
	/*{
		vector<PB>_;
		V.swap(_);
		PB temp; V.push_back(temp);
	}*/
//	V[0].insert(1);
//	V[0].insert(2);
//	cerr<< (*V[0].find_by_order(0))<<endl;
	tot=0;
	
	tp=0;
	for(int i=1;i<=n;i++)
	{
		t[++tp]=i;
		loc=a[i]; root[i]=ins(root[i-1],1,n);
//		PB ti; ti.insert(a[i]);
//		V.push_back(ti);
		
		while(tp>1)
		{
			ll k1= calc(t[tp-1],t[tp]-1);
			ll k2= calc(t[tp],i);
			/*
			cerr<<"tp-1:  ";
			for(int k=0;k<(int)V[tp-1].size();k++) cerr<<*V[tp-1].find_by_order(k)<<' ';
			cerr<<endl;
			cerr<<"tp:    ";
			for(int k=0;k<(int)V[tp].size();k++) cerr<<*V[tp].find_by_order(k)<<' ';
			cerr<<endl;
			*/
			if(k1>k2) 
			{
			//	V[tp-1].join(V[tp]);
			//	V.pop_back();
				tp--;
				/*cerr<<"merge: ";
				for(int k=0;k<(int)V[tp].size();k++) cerr<<*V[tp].find_by_order(k)<<' ';
				cerr<<endl;*/
			}
			else break;
		}
	}
	t[tp+1]=n+1;
	
	ll ret=0;
	int now=0;ll mid;
	for(int i=1;i<=n;i++)
	{
		if(i>=t[now+1] && now<tp) now++, mid=calc(t[now],t[now+1]-1);
		ret+= abs( ai[ a[i] ].first - ai[mid].first );
	}
	return ret;
}
int sum[maxn];
void add(int x,int c)
{
	for(;x<=n;x+=lowbit(x)) sum[x]+=c;
}
int query(int x)
{
	int re=0;
	for(;x;x-=lowbit(x)) re+=sum[x];
	return re;
}

int getmid(int l,int r)
{
	vector<int>V;
	for(int i=l;i<=r;i++) V.push_back(a[i]);
	sort(V.begin(),V.end());
	int num=r-l+1;
	return V[ (num+1)/2-1 ];
}
int f[100][100];
ll solve2()
{
	f[0][0]=0;
	for(int i=1;i<=n;i++) 
	{
		for(int c=0;c<=n;c++) f[i][c]=LLONG_MAX;
		for(int j=0;j<i;j++) 
		{
			ll cc=0;
			int mid=getmid(j+1,i);
			for(int k=j+1;k<=i;k++)
			{
				cc+= abs( ai[mid].first-ai[ a[k] ].first );
			}
			for(int c=0;c<=mid;c++) if(f[j][c]!=LLONG_MAX)
			{
				f[i][mid]=min(f[i][mid], f[j][c]+cc);
			}
			
		}
	}
	int ans=LLONG_MAX;
	for(int c=1;c<=n;c++) ans=min(ans,f[n][c]);
	return ans;
}

signed main()
{
	/////////////////////////////////////////////////
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	mt19937 mt((unsigned long long)(new char));
	
	int Tcase=1000;
	cin>>Tcase;
	while(Tcase--)
	{
		int K=1;
		n=20;
		cin>>n>>K;
		for(int i=1;i<=n;i++) 
		{
			a[i]=mt()%1000+1;
			cin>>a[i];
		}
		
		//cerr<<n<<endl;
		//for(int i=1;i<=n;i++) cerr<<a[i]<<' ';
		//cerr<<endl;
		
		for(int i=1;i<=n;i++)
		{
			ai[i]=make_pair(a[i],i);
		}
		sort(ai+1,ai+n+1);
		for(int i=1;i<=n;i++) a[ai[i].second]=i;
		
		if(K==1)
		{
			ll c1=solve(), c2=solve2();
			assert( c1==c2 );
			cout<<solve()<<'\n';
		}
		else if(K==n)
		{
			int ans=LLONG_MAX;
			for(int d=0;d<n;d++)
			{
				rotate(a+1,a+2,a+n+1);
				ans=min(ans, solve());
			}
			cout<<ans<<'\n';
		}
		else
		{
			if(K%2==0) cout<<"0\n";
			else
			{
				int num=0;
				for(int i=1;i<=n;i++) sum[i]=0;
				for(int i=1;i<=n;i++)
				{
					num+= (i-1)-query(a[i]);
					add(a[i],1);
				}
				if(num%2==1)
				{
					ll ans=LLONG_MAX;
					for(int i=2;i<=n;i++) ans=min(ans,ai[i].first-ai[i-1].first);
					cout<<ans<<'\n';
				}
				else cout<<"0\n";
			}
		}
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 16ms
memory: 243464kb

input:

4
4 1
6 4 3 7
4 2
6 4 3 7
4 3
6 4 3 7
4 4
6 4 3 7

output:

3
0
1
2

result:

ok 4 number(s): "3 0 1 2"

Test #2:

score: 0
Accepted
time: 20ms
memory: 244728kb

input:

10000
4 3
524728 254456 277709 19127
15 11
360089 525234 862619 897281 336644 910706 75922 708901 754517 734744 94169 326125 746826 846063 159956
4 2
140105 792522 40264 514789
12 2
270333 888927 500833 9065 936673 982631 332435 751429 607700 840339 804685 416612
8 7
119416 689632 517277 673646 8262...

output:

23253
7691
0
0
15986
278544
0
0
0
0
0
2022
0
0
0
9260
0
0
51255
0
0
277173
480146
0
658
4525
0
0
0
0
0
266148
0
767231
5853
0
0
121885
0
788638
0
0
0
779611
0
5881
0
0
0
0
517074
0
0
0
210836
454586
662851
0
781542
0
0
864957
175421
0
0
0
0
0
0
0
541010
0
0
15407
0
0
3413333
0
0
0
0
19677
30305
3095...

result:

ok 10000 numbers

Test #3:

score: 0
Accepted
time: 28ms
memory: 244372kb

input:

10000
1 1
2246872
10 1
6956989 9799253 5103131 200665 8599026 7743840 6622177 9299599 4771199 2388897
1 1
4115997
2 1
2246219 2946703
1 1
8870887
5 4
4465846 6917492 4431029 8967539 33631
11 11
5721411 592798 6930331 6862401 4082972 2094477 1505423 2091687 9125024 8518457 361077
4 2
8818946 4073615 ...

output:

0
23312638
0
0
0
0
17919297
0
543116
0
0
783241
0
44991
0
0
0
4721212
0
11367749
0
0
421992
0
4267974
0
0
0
0
0
0
0
0
1172214
0
0
0
0
0
9209019
0
0
5365348
922347
463528
10588447
0
0
0
2144103
17190623
19634388
142708
6877382
0
0
0
0
0
0
0
0
0
1477236
0
0
0
0
0
0
820573
0
0
0
3767488
8960620
0
10561...

result:

ok 10000 numbers

Test #4:

score: 0
Accepted
time: 28ms
memory: 242960kb

input:

10000
2 1
45149197 33261068
5 1
55118470 16401191 17389202 89510782 34998353
5 5
63464501 21878886 62995140 27832883 54891420
7 2
85638582 825401 81784814 21532809 30150850 21800436 94882138
2 2
83299527 63718695
3 1
89482904 64518505 91301116
1 1
82256621
1 1
30148988
3 1
68107859 50635233 8682010
...

output:

11888129
93229708
35162257
0
0
24964399
0
0
59425849
0
22479259
5248308
0
0
0
41327792
0
207654
0
0
0
0
0
0
68210620
0
0
194925
0
0
73467281
33859825
122226
74005621
26201400
0
119179402
0
0
0
0
0
0
0
816171
0
0
0
25910307
3451662
0
4390900
0
0
0
22895459
0
0
0
102364933
0
346465
0
0
0
0
0
58190487
...

result:

ok 10000 numbers

Test #5:

score: 0
Accepted
time: 20ms
memory: 243472kb

input:

10000
3 2
171561989 326460559 568826834
4 1
606735910 34072129 203284467 873417326
1 1
436249551
3 2
866901680 525830568 142746353
14 11
742357529 676987595 673771185 430647518 327098063 734643932 300528112 859509055 593973771 842011838 467635682 368399037 810057370 576054534
3 2
822197945 247906143...

output:

0
572663781
0
0
3216410
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
696031608
0
559141551
975330575
0
0
0
0
8269118
0
1645243801
0
0
0
0
0
480200189
444013173
1223177234
0
17211999
0
0
0
364069727
178337070
5296068017
0
0
0
0
709411979
0
2420843006
192213554
238004067
0
0
0
0
0
0
0
0
2626223
249160097
1183427809
...

result:

ok 10000 numbers

Test #6:

score: -100
Runtime Error

input:

10000
11 1
719994 371444 177473 165906 258003 388506 556396 344249 756298 954668 668450
37 1
154783 680471 27958 18537 684073 711211 924310 535353 766034 510375 800832 64509 814950 546211 134844 3096 877063 837800 722279 626142 168108 336054 747182 590551 161949 182719 495638 869503 252408 315050 73...

output:


result: