QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#810435#978. Maximum Subsequencerlc202204TL 1883ms4472kbC++143.1kb2024-12-11 22:28:532024-12-11 22:29:02

Judging History

This is the latest submission verdict.

  • [2024-12-11 22:29:02]
  • Judged
  • Verdict: TL
  • Time: 1883ms
  • Memory: 4472kb
  • [2024-12-11 22:28:53]
  • 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);
}
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];

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 i = 1; i <= L; i++)
		p[i] = i;
	do {
		for (int i = 1; i <= L; i++)
			tmp[i] = a[p[i]];
		pii ttt = slv2(tmp, L);
		if (ttt.first <= ANS)
			A[++tl] = ttt;
	} 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]];
		pii ttt = slv1(tmp, R);
		if (ttt.first <= ANS)
			B[++tr] = ttt;
	} while (next_permutation(p + 1, p + R + 1));
//	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 (n > 12 && tot % 11 != 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);
	dfs(1);
	cout << ANS << endl;
	return 0;
} 
/*
 6
 4 -4 5 -20 6 7
*/

詳細信息

Test #1:

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

input:

4
1 -1 1 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

6
4 -4 5 -20 6 7

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 1183ms
memory: 4460kb

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: 525ms
memory: 4228kb

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: 40ms
memory: 3772kb

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: 0
Accepted
time: 598ms
memory: 4252kb

input:

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

output:

547690

result:

ok 1 number(s): "547690"

Test #7:

score: 0
Accepted
time: 1856ms
memory: 4400kb

input:

16
-95394 27873 82707 -8091 22839 -22353 77819 17786 -20965 -17390 21878 2116 44799 -4573 16174 7845

output:

153070

result:

ok 1 number(s): "153070"

Test #8:

score: 0
Accepted
time: 968ms
memory: 4328kb

input:

16
73674 -66214 -39831 88784 42617 -75096 85579 1073 -55675 82743 -85078 -88198 12378 -14310 20005 33627

output:

88784

result:

ok 1 number(s): "88784"

Test #9:

score: 0
Accepted
time: 1243ms
memory: 4280kb

input:

16
-83517 -65495 8945 31495 -30343 94018 14500 -13614 68884 -45706 -18744 -67271 93093 -14098 -3592 -58773

output:

94018

result:

ok 1 number(s): "94018"

Test #10:

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

input:

6
-15003 -28170 -30377 81020 -44906 -21965

output:

81020

result:

ok 1 number(s): "81020"

Test #11:

score: 0
Accepted
time: 1233ms
memory: 4348kb

input:

16
-29848 4832 94117 -45241 1204 -99570 -10513 97070 48069 43198 -13260 41687 -57361 33422 -86646 -15135

output:

97070

result:

ok 1 number(s): "97070"

Test #12:

score: 0
Accepted
time: 564ms
memory: 4120kb

input:

15
-40154 98402 72394 -18077 89858 -8243 82205 85533 18703 99891 64337 -40784 82754 62837 78715

output:

728371

result:

ok 1 number(s): "728371"

Test #13:

score: 0
Accepted
time: 36ms
memory: 3900kb

input:

14
66250 -35079 -57181 19720 81648 35074 32470 58509 53389 -57108 -98486 10518 50384 -93648

output:

86765

result:

ok 1 number(s): "86765"

Test #14:

score: 0
Accepted
time: 1729ms
memory: 4472kb

input:

16
40899 86787 42827 46271 50611 5024 18935 -29778 -63775 50497 71624 66733 58611 11007 3100 95197

output:

554570

result:

ok 1 number(s): "554570"

Test #15:

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

input:

5
-45688 73098 -92315 33790 -13955

output:

73098

result:

ok 1 number(s): "73098"

Test #16:

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

input:

3
98693 1701 31884

output:

132278

result:

ok 1 number(s): "132278"

Test #17:

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

input:

1
28298

output:

28298

result:

ok 1 number(s): "28298"

Test #18:

score: 0
Accepted
time: 540ms
memory: 4012kb

input:

15
11823 66662 -54420 37824 22297 92212 69303 -43475 29895 90424 46778 59451 -6719 73011 3205

output:

498271

result:

ok 1 number(s): "498271"

Test #19:

score: 0
Accepted
time: 660ms
memory: 4256kb

input:

15
10336 -51879 68382 87402 -30237 -62771 80927 -15568 -6831 38917 98576 -18210 20823 66361 -11231

output:

274997

result:

ok 1 number(s): "274997"

Test #20:

score: 0
Accepted
time: 1883ms
memory: 4332kb

input:

16
-35603 19466 4413 -9913 48173 31103 35653 91725 -92984 73019 78100 38659 53651 53436 37700 17258

output:

443856

result:

ok 1 number(s): "443856"

Test #21:

score: 0
Accepted
time: 56ms
memory: 3908kb

input:

14
92942 63046 76091 19008 62272 27814 85894 -8202 28803 70386 29703 72309 -8672 -29761

output:

581633

result:

ok 1 number(s): "581633"

Test #22:

score: -100
Time Limit Exceeded

input:

16
-50703 -66984 48586 -6141 -15903 -27332 57733 90767 46085 69464 68558 33327 89292 95868 62116 -72578

output:


result: