QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#810422 | #978. Maximum Subsequence | rlc202204 | TL | 1877ms | 4448kb | C++14 | 2.9kb | 2024-12-11 22:20:33 | 2024-12-11 22:20:38 |
Judging History
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);
int ans = 2e9;
for (int i = 1, j = 0; i <= tl; i++) {
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++) {
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 chose[N] = {0};
bool vis[N] = {false};
int tot = 0;
void dfs(int x) {
if (x > (n + 1) / 2) {
++tot;
if (n > 12 && tot % 7 != 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() {
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: 3900kb
input:
4 1 -1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3692kb
input:
6 4 -4 5 -20 6 7
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: 0
Accepted
time: 1877ms
memory: 4448kb
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: 810ms
memory: 4172kb
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: 60ms
memory: 3976kb
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: 912ms
memory: 4116kb
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: -100
Time Limit Exceeded
input:
16 -95394 27873 82707 -8091 22839 -22353 77819 17786 -20965 -17390 21878 2116 44799 -4573 16174 7845