QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#514742#5507. InvestorsqqqaaazzzML 1ms3668kbC++141.5kb2024-08-11 09:30:272024-08-11 09:30:28

Judging History

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

  • [2024-08-11 09:30:28]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:3668kb
  • [2024-08-11 09:30:27]
  • 提交

answer

#include <bits/stdc++.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
int n,k;
int a[6010];
int val[6010][6010];
int f[6010],g[6010];
map<int,int> mp;
int idx;
int t[6010];
const int inf = 1e9;
int lowbit(int x){
	return (x&-x);
}
void add(int x,int c){
	while(x<=idx){
		t[x] += c;
		x += lowbit(x);
	}
	return;
}
int query(int x){
	int res = 0;
	while(x){
		res += t[x];
		x -= lowbit(x);
	}
	return res;
}
void fun(int st){
	for (int i=0;i<=idx;i++) t[i] = 0;
	for (int i=st;i<=n;i++){
		val[st][i] = val[st][i-1]+(query(idx)-query(a[i]));
		add(a[i],1);
	}
	return;
}
void solve(int l,int r,int L,int R){
	int mid = (l+r)/2;
	int pos = 0;
	for (int i=L;i<min(R+1,mid);i++){
		if(g[i]+val[i+1][mid]<f[mid]){
			f[mid] = g[i]+val[i+1][mid];
			pos = i;
		}
	}
	//cout << l << " " << r << " " << pos << "\n";
	if(l==r) return;
	solve(l,mid-1,L,pos);
	solve(mid+1,r,pos,R);
	return; 
}

void solve(){
	mp.clear();
	idx = 0;
	cin >> n >> k;
	++k;
	for (int i=1;i<=n;i++){
		cin >> a[i];
		mp[a[i]] = 0;
	}
	for (auto &i : mp) i.second = ++idx;
	for (int i=1;i<=n;i++) a[i] = mp[a[i]];
	for (int i=1;i<=n;i++) fun(i);
	for (int i=1;i<=n;i++) f[i] = inf;
	f[0] = 0;
	for (int i=1;i<=k;i++){
		for (int j=0;j<=n;j++){
			g[j] = f[j];
			f[j] = inf;
		}
		solve(0,n,0,n);
	}
	cout << f[n] << "\n";
	return;
}

signed main()
{
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3668kb

input:

2
6 1
4 5 6 2 2 1
6 2
4 5 6 2 2 1

output:

2
0

result:

ok 2 lines

Test #2:

score: -100
Memory Limit Exceeded

input:

349
6 2
2 1 2 1 2 1
48 12
42 47 39 39 27 25 22 44 45 13 5 48 38 4 37 6 24 10 42 38 12 37 31 19 44 6 29 17 7 12 7 26 35 24 15 9 37 3 27 21 33 29 34 20 14 30 31 21
48 12
1 43 17 46 17 39 40 22 25 2 22 12 4 11 38 12 4 11 1 5 39 44 37 10 19 20 42 45 2 45 37 20 48 34 16 41 23 18 13 44 47 21 29 4 23 18 16...

output:

1

result: