# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
534006 |
2022-03-07T19:03:59 Z |
idas |
Feast (NOI19_feast) |
C++11 |
|
400 ms |
13072 KB |
#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
#define all(x) (x).begin(), (x).end()
#define le(vec) vec[vec.size()-1]
#define sz(x) ((int)((x).size()))
#define pb push_back
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e13;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
#define ll ld
const int N=3e5+10;
int n, k, a[N], cn[N][2];
ll dp[N][2];
bool can(ll x)
{
FOR(i, 0, n+1) FOR(j, 0, 2) dp[i][j]=cn[i][j]=0;
dp[0][1]=-INF;
FOR(i, 1, n+1)
{
dp[i][0]=max(dp[i-1][0], dp[i-1][1]);
if(dp[i-1][0]>dp[i-1][1]){
cn[i][0]=cn[i-1][0];
}
else if(cn[i-1][0]<dp[i-1][1]){
cn[i][0]=cn[i-1][1];
}
else{
cn[i][0]=min(cn[i-1][0], cn[i-1][1]);
}
dp[i][1]=max(dp[i-1][0]+a[i-1]-x, dp[i-1][1]+a[i-1]);
if(dp[i-1][0]-x>dp[i-1][1]){
cn[i][1]=cn[i-1][0]+1;
}
else if(dp[i-1][0]-x<dp[i-1][1]){
cn[i][1]=cn[i-1][1];
}
else{
cn[i][1]=min(cn[i-1][0]+1, cn[i-1][1]);
}
}
if(dp[n][0]>dp[n][1]){
return cn[n][0]<=k;
}
else if(dp[n][1]>dp[n][0]){
return cn[n][1]<=k;
}
else{
return min(cn[n][0], cn[n][1])<=k;
}
}
ll build(ll x)
{
FOR(i, 0, n+1) FOR(j, 0, 2) dp[i][j]=cn[i][j]=0;
dp[0][1]=-INF;
FOR(i, 1, n+1)
{
dp[i][0]=max(dp[i-1][0], dp[i-1][1]);
if(dp[i-1][0]>dp[i-1][1]){
cn[i][0]=cn[i-1][0];
}
else if(cn[i-1][0]<dp[i-1][1]){
cn[i][0]=cn[i-1][1];
}
else{
cn[i][0]=min(cn[i-1][0], cn[i-1][1]);
}
dp[i][1]=max(dp[i-1][0]+a[i-1]-x, dp[i-1][1]+a[i-1]);
if(dp[i-1][0]-x>dp[i-1][1]){
cn[i][1]=cn[i-1][0]+1;
}
else if(dp[i-1][0]-x<dp[i-1][1]){
cn[i][1]=cn[i-1][1];
}
else{
cn[i][1]=min(cn[i-1][0]+1, cn[i-1][1]);
}
}
return max(dp[n][0]+cn[n][0]*x, dp[n][1]+cn[n][1]*x);
}
int main()
{
setIO();
cin >> n >> k;
FOR(i, 0, n) cin >> a[i];
const ld eps=1e-6;
ld l=0, r=INF;
while(r-l>eps){
ld m=(l+r)/2;
if(can(m)) r=m;
else l=m+1;
}
cout << build(l);
}
Compilation message
feast.cpp: In function 'void setIO(std::string)':
feast.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
400 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
255 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
320 ms |
13072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
400 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |