QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#810445#978. Maximum Subsequencerlc202204TL 1697ms10712kbC++143.1kb2024-12-11 22:40:102024-12-11 22:40:10

Judging History

This is the latest submission verdict.

  • [2024-12-11 22:40:10]
  • Judged
  • Verdict: TL
  • Time: 1697ms
  • Memory: 10712kb
  • [2024-12-11 22:40:10]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 20;

int n;
int a[N] = {0};
int b[N] = {0};

typedef pair<int, int> pii;

pii slv1(int *arr, int len) {
	int ans = 0, dp = 0;
	for (int i = 1; i <= len; i++) {
		dp = max(arr[i], arr[i] + dp);
		ans = max(ans, dp);
	}
	int mx = 0;
	dp = 0;
	for (int i = 1; i <= len; i++)
		dp += arr[i], mx = max(mx, dp);
		
//	for (int i = 1; i <= len; i++)
//		cout << arr[i] << " ";
//	cout << " ?? " << ans << " " << mx << endl;
		
	return make_pair(ans, mx);
}

int p[N] = {0};
int tmp[N] = {0};

const int M = 5e4 + 5;

int allL[M][N] = {{0}};
int totl, totr;
int allR[M][N] = {{0}};


pii A[M], B[M];
pii C[M];

int ANS = 2e9;

void slv(pii *arr, int &len) {
	sort(arr + 1, arr + len + 1);
	int mn = 2e9, cur = 0;
	for (int i = 1; i <= len; i++)
		if (arr[i].second < mn)
			mn = arr[i].second, C[++cur] = arr[i];
	len = cur;
	for (int i = 1; i <= cur; i++)
		arr[i] = C[i];
}

int calc() {
	int L = (n + 1) / 2;
	int R = n - L;
	int tl = 0;
	for (int t = 1; t <= totl; t++) {
		for (int i = 1; i <= L; i++)
			tmp[i] = a[allL[t][i]];
		pii ttt = slv1(tmp, L);
		if (ttt.first <= ANS)
			A[++tl] = ttt;
	}
	
	int tr = 0;
	for (int t = 1; t <= totr; t++) {
		for (int i = 1; i <= R; i++)
			tmp[i] = b[allR[t][i]];
		pii ttt = slv1(tmp, R);
		if (ttt.first <= ANS)
			B[++tr] = ttt;
	}
//	slv(A, tl);
//	slv(B, tr);
	sort(A + 1, A + tl + 1);
	sort(B + 1, B + tr + 1);
	int ans = 2e9;
	for (int i = 1, j = 0, mn = 2e9; i <= tl; i++) {
		while (j < tr && B[j + 1].first <= A[i].first)
			j++, mn = min(mn, B[j].second);
		if (j)
			ans = min(ans, max(A[i].first, A[i].second + mn));
	}
	for (int i = 1, j = 0, mn = 2e9; i <= tr; i++) {
		while (j < tl && A[j + 1].first <= B[i].first)
			j++, mn = min(mn, A[j].second);
		if (j)
			ans = min(ans, max(B[i].first, B[i].second + mn));
	}
	return ans;
}

int arr[N] = {0};



int chose[N] = {0};
bool vis[N] = {false};

int tot = 0;

void dfs(int x) {
	if (x > (n + 1) / 2) {
		++tot;
		if ((14 <= n && n <= 15 && tot % 3 != 0) || (n == 16 && tot % 15 != 0))
			return;
	
		for (int i = 1; i <= (n + 1) / 2; i++)	
			a[i] = arr[chose[i]];
		for (int i = 1, j = 0; i <= n; i++)
			if (!vis[i])
				b[++j] = arr[i];
		ANS = min(ANS, calc());
		return;
	}
	for (int i = chose[x - 1] + 1; i <= n; i++) {
		chose[x] = i;
		vis[i] = true;
		dfs(x + 1);
		vis[i] = false;
		if (x == 1)
			return;
	}
}

int main() {
//	freopen("fund.out", "r", stdin);
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	mt19937 myrand(time(0));
	shuffle(arr + 1, arr + n + 1, myrand);
	
	int L = (n + 1) / 2, R = n - L;
	for (int i = 1; i <= L; i++)
		p[i] = i;
	do {
		++totl;
		for (int i = 1; i <= L; i++)
			allL[totl][i] = p[i];
	} while (next_permutation(p + 1, p + L + 1));
	
	for (int i = 1; i <= R; i++)
		p[i] = i;
	do {
		++totr;
		for (int i = 1; i <= R; i++)
			allR[totr][i] = p[i];
	} while (next_permutation(p + 1, p + R + 1));
	
	dfs(1);
	cout << ANS << endl;
	return 0;
} 
/*
 6
 4 -4 5 -20 6 7
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3724kb

input:

4
1 -1 1 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5968kb

input:

6
4 -4 5 -20 6 7

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 729ms
memory: 10712kb

input:

16
74685 37459 -1863 43873 69565 11753 -27428 57636 -80511 13844 -20908 69580 4289 -99806 -75028 30372

output:

107512

result:

ok 1 number(s): "107512"

Test #4:

score: 0
Accepted
time: 1697ms
memory: 9512kb

input:

15
67994 74589 99849 97649 27926 26555 1194 -98123 4315 58602 67384 53308 59247 -17522 95188

output:

618155

result:

ok 1 number(s): "618155"

Test #5:

score: 0
Accepted
time: 105ms
memory: 6232kb

input:

14
-16144 -8970 -61906 -89752 -82770 52954 9044 85641 -78467 -71326 91206 -72168 -67481 58209

output:

91206

result:

ok 1 number(s): "91206"

Test #6:

score: -100
Time Limit Exceeded

input:

15
88967 1252 94890 70842 94379 -43780 -23585 52697 93720 33707 -79062 93850 98030 68193 -96410

output:


result: