#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%lld %lld",&a, &b);
#define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
const int N = 1e7+11,INF=1e9+7;
int dp[10000101],dp1[10000101],q[N],n;
int rec(int sum,int cnt,int j){
int i,f=0;
int h = (sum*41+(cnt*1681)%N)%N;
int h1 = (sum*79+(cnt*6241)%N)%N;
if (dp[h] == dp1[h1] && dp[h]>0)ret dp[h];
for (i=1;i<=n;++i){
if (q[i] > 0 && q[i-1] > 0)f=1;
}
if (!f){
int x = 0;
for (i=1;i<=n;++i)x+=q[i];
sum+=x*3;
ret sum;
}
int ans = INF;
for (i=1;i<=n;++i){
if (q[i] <= 0)continue;
int mn = q[i];
q[i]-=mn;
ans = min(ans,rec(sum+mn*3,cnt+1,i));
q[i]+=mn;
if (i > 1&& q[i-1] > 0){
if (q[i-1] <= q[i]){
mn = q[i-1];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*5,cnt+1,i));
q[i-1]+=mn;
q[i]+=mn;
}
else {
mn = q[i];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*5,cnt+1,i));
q[i-1]+=mn;
q[i]+=mn;
}
}
if (i > 2 && q[i-2] > 0 && q[i-1] > 0){
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*7,cnt+1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-2] >= q[i] && q[i] <= q[i-1]){
mn = q[i];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*7,cnt+1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(sum+mn*7,cnt+1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
}
}
ret dp[h]= dp1[h1] = ans;
}
main(){
int i,ans=INF;
scan1(n)
for (i=1;i<=n;++i){
scan1(q[i])
}
i=1;
int mn = q[i];
q[i]-=mn;
ans = min(ans,rec(mn*3,1,i));
q[i]+=mn;
i++;
if (i > 1){
if (q[i-1] <= q[i]){
mn = q[i-1];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*5,1,i));
q[i-1]+=mn;
q[i]+=mn;
}
else {
mn = q[i];
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*5,1,i));
q[i-1]+=mn;
q[i]+=mn;
}
}
i++;
if (i > 2){
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*7,1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-2] >= q[i] && q[i] <= q[i-1]){
mn = q[i];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*7,1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
if (q[i-1] <= q[i] && q[i-1] <= q[i-2]){
mn = q[i-1];
q[i-2]-=mn;
q[i-1]-=mn;
q[i]-=mn;
ans = min(ans,rec(mn*7,1,i));
q[i-2]+=mn;
q[i-1]+=mn;
q[i]+=mn;
}
}
cout <<ans;
}
Compilation message
skyline.cpp:99:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
99 | main(){
| ^
skyline.cpp: In function 'int main()':
skyline.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
11 | #define scan1(a) scanf("%lld",&a);
| ~~~~~^~~~~~~~~~~
skyline.cpp:101:5: note: in expansion of macro 'scan1'
101 | scan1(n)
| ^~~~~
skyline.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
11 | #define scan1(a) scanf("%lld",&a);
| ~~~~~^~~~~~~~~~~
skyline.cpp:103:9: note: in expansion of macro 'scan1'
103 | scan1(q[i])
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Incorrect |
82 ms |
6252 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |