QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115179 | #5851. Travel Plan | zhouhuanyi | 33 ✓ | 1636ms | 26692kb | C++11 | 1.5kb | 2023-06-24 20:14:37 | 2023-06-24 20:14:40 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 30
using namespace std;
long long read()
{
char c=0;
long long sum=0,f=1;
while (c!='-'&&(c<'0'||c>'9')) c=getchar();
if (c=='-') c=getchar(),f=-1;
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum*f;
}
int T,n,Base;
const long long inf=(long long)(1e18);
long long a[N+1],F,ans;
vector<long long>p[N+1];
vector<long long>q[N+1];
void dfs(int x,int d,long long res)
{
if (x==Base+1)
{
p[d].push_back(res);
return;
}
if (x==1||d) dfs(x+1,d+1,res-(a[x]<<1));
if (d) dfs(x+1,d,res),dfs(x+1,d-1,res+(a[x]<<1));
return;
}
void dfs2(int x,int d,long long res)
{
if (x==Base)
{
q[d].push_back(res);
return;
}
if (x==n||d) dfs2(x-1,d+1,res+(a[x]<<1));
if (d) dfs2(x-1,d,res),dfs2(x-1,d-1,res-(a[x]<<1));
return;
}
int main()
{
int ps;
T=read();
for (int qt=1;qt<=T;++qt)
{
n=read(),Base=n>>1,ans=-inf;
for (int i=1;i<=n;++i) a[i]=read();
F=read(),sort(a+1,a+n+1);
for (int i=0;i<=Base;++i) p[i].clear(),q[i].clear();
dfs(1,0,0),dfs2(n,0,0);
for (int i=1;i<=Base;++i)
{
sort(p[i].begin(),p[i].end()),sort(q[i].begin(),q[i].end()),ps=-1;
for (int j=(int)(q[i].size())-1;j>=0;--j)
{
while (ps+1<p[i].size()&&p[i][ps+1]+q[i][j]<=F) ++ps;
if (ps!=-1) ans=max(ans,p[i][ps]+q[i][j]);
}
}
if (ans==-inf) printf("Case #%d: NO SOLUTION\n",qt);
else printf("Case #%d: %lld\n",qt,ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 3
Accepted
Test #1:
score: 3
Accepted
time: 1ms
memory: 3480kb
input:
100 3 0 10 -10 40 5 0 1 2 3 4 13 5 0 1 2 3 4 7 8 0 789059689301482 -910931693858101 -956193093955857 343509202930243 920374185149989 -689955229424826 722030552663468 482269012678218 7 0 999999999999544 999999999999389 999999999999930 -999999999999063 -999999999999192 -999999999999596 417587359369470...
output:
Case #1: 40 Case #2: 12 Case #3: NO SOLUTION Case #4: NO SOLUTION Case #5: 3999999999999620 Case #6: 4051752 Case #7: NO SOLUTION Case #8: NO SOLUTION Case #9: 1999999999998958 Case #10: 7010924229270316 Case #11: 4595167479146090 Case #12: 3500897892661482 Case #13: NO SOLUTION Case #14: NO SOLUTIO...
result:
ok 100 lines
Subtask #2:
score: 30
Accepted
Test #2:
score: 30
Accepted
time: 1636ms
memory: 26692kb
input:
20 28 0 -296203321911394 -669869184126344 878407950821263 -835889965067355 421301173991167 -801344314893054 403607682072382 227003460040564 154808607155152 767043420856216 -346030550495039 -992039850227156 -785288091280455 -466542633673897 -216447853885253 -183726687442679 443056439241543 6555553252...
output:
Case #1: 16804381377370010 Case #2: NO SOLUTION Case #3: 13032281145308020 Case #4: 17070915940795392 Case #5: NO SOLUTION Case #6: NO SOLUTION Case #7: 18456730 Case #8: 19800000000000000 Case #9: 37999999999977270 Case #10: NO SOLUTION Case #11: 9244607752 Case #12: 20372686 Case #13: NO SOLUTION ...
result:
ok 20 lines
Extra Test:
score: 0
Extra Test Passed