QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#810375#978. Maximum Subsequencerlc202204TL 0ms3728kbC++172.8kb2024-12-11 21:42:002024-12-11 21:42:02

Judging History

This is the latest submission verdict.

  • [2024-12-11 21:42:02]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3728kb
  • [2024-12-11 21:42:00]
  • Submitted

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
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);
}
pii slv2(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 = len; i >= 1; 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;
pii A[M], B[M];
pii C[M];

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 i = 1; i <= L; i++)
		p[i] = i;
	do {
		for (int i = 1; i <= L; i++)
			tmp[i] = a[p[i]];
		A[++tl] = slv2(tmp, L);
	} while (next_permutation(p + 1, p + L + 1));
	
	int tr = 0;
	for (int i = 1; i <= R; i++)
		p[i] = i;
	do {
		for (int i = 1; i <= R; i++)
			tmp[i] = b[p[i]];
		B[++tr] = slv1(tmp, R);
	} while (next_permutation(p + 1, p + R + 1));
	
	
	slv(A, tl);
	slv(B, tr);
	int ans = 2e9;
	for (int i = 1, j = 0; i <= tl; i++) {
	//	cout << i << " " << A[i].first << " " << A[i].second << endl;
		while (j < tr && B[j + 1].first <= A[i].first)
			j++;
		if (j)
			ans = min(ans, max(A[i].first, A[i].second + B[j].second));
	}
	for (int i = 1, j = 0; i <= tr; i++) {
	//	cout << i << " " << B[i].first << " " << B[i].second << endl;
		while (j < tl && A[j + 1].first <= B[i].first)
			j++;
		if (j)
			ans = min(ans, max(B[i].first, B[i].second + A[j].second));
	}
	return ans;
}

int arr[N] = {0};

int ans = 2e9;

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

void dfs(int x) {
	if (x > (n + 1) / 2) {
		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;
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
	dfs(1);
	cout << ans << endl;
	return 0;
} 
/*
 6
 4 -4 5 -20 6 7
*/

詳細信息

Test #1:

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

input:

4
1 -1 1 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3728kb

input:

6
4 -4 5 -20 6 7

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: -100
Time Limit Exceeded

input:

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

output:


result: