이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <chrono>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
const int MAXN = 3e5+3;
ll arr[MAXN];
pair<long double, ll> mem[MAXN][2];
ll n, k;
bool isVisited[MAXN][2];
long double lambda;
pair<long double, ll> dp(ll index, int inInterval){
if (index == n) return {0, 0};
if (isVisited[index][inInterval])return mem[index][inInterval];
isVisited[index][inInterval] = true;
if (inInterval){
//fortsätt -> ingen penalty
pair<long double, ll> cont = {dp(index+1, 1).first+arr[index], dp(index+1, 1).second};
pair<long double, ll> stop = {dp(index+1, 0).first, dp(index+1, 0).second};
//stanna -> penalty
return mem[index][inInterval] = max(cont, stop);
} else {
//köp inget
pair<long double, ll> cont = {dp(index+1, 0).first, dp(index+1, 0).second};
//starta
pair<long double, ll> start = {dp(index+1, 1).first + arr[index] - lambda, dp(index+1, 1).second+1};
return mem[index][inInterval] = max(cont, start);
}
}
bool check(long double la){
lambda = la;
for (int i = 0; i < n; i++){
isVisited[i][0] = false;
isVisited[i][1] = false;
}
//cout << la << " " << dp(0, 0).first << " " << dp(0, 0).second << endl;
return dp(0,0).second >= k;
}
int main(){
scoobydoobydoo();
cin >> n >> k;
for (int i = 0; i < n; i++)cin >> arr[i];
long double a = 0, b = 1e15;
chrono::time_point<chrono::system_clock> start, end;
start = chrono::system_clock::now();
while (a+(1e-3) < b){
long double mid = (a+b)/2.0;
if (check(mid)){
a = mid;
} else {
b = mid;
}
end = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = end - start;
if (elapsed_seconds.count() > 0.69)break;
}
check(a);
//cout << fixed << setprecision(5);
//cout << a << endl;
//cout << check(6.99) << endl;
//cout << check(7.99) << endl;
ll ans = round(a*k + mem[0][0].first);
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |