P17795 [入门赛 #53] 新能源之旅(二答 + 贪心)
P17795 [入门赛 #53] 新能源之旅(二答 + 贪心)
我绝对不会告诉你我赛时只做了 F 题,反正没开 rated。
简述题意
给你 \(n\) 段道路和 \(k\) 个备用电池,每段道路要花费 \(a_i\)(当前电量必须大于等于 \(a_i\) 才可通过)再充 \(b_i\)(注意只有最开始的电池可以充电)如果电量不够了,就要换掉这个电池,换上新的,且换掉的电池不能再用,要求出最小的电池容量 \(C\)(最开始的电池容量和 \(k\) 个备用电池的容量都是 \(C\))。
题目解析
题意很明显啊,首先,看到求最小值想到 二分答案。
接下来考虑 check 怎么写:
其实就想到于给定 \(C\) 判断是否可行对吧,我们直接根据题意模拟,如果不够了,就换上备用电池,然后就是注意处理备用电池数量和备用电池不可充电,就没了。
注意事项
- 注意起始电池的 充电上限。
- 注意二分的区间 \([L, R]\) 这里建议给 \([1, 10^{18}]\)。
- 十年 OI 一场空,________________。
代码示例
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<map>
#include<unordered_map>
#include<ctime>
#include<random>
#define StreamSync
#define int long long
using namespace std;
bool START;
const int N = 1e6 + 10;
const int M = 5010;
const int mod = 1e9 + 7;
const double eps = 1e-9;
typedef long long LL;
typedef unsigned long long ULL;
typedef double db;
typedef pair<int, int> PII;
inline int read(){
int x = 0, f = 1; char ch = getchar();
while(!isdigit(ch)){if(ch == '-') f = -f; ch = getchar();}
while(isdigit(ch)){x = (x << 1) + (x << 3) + ch - '0'; ch = getchar();}
return x * f;
}
inline void write(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x % 10);
putchar('0' + x % 10);
}
int n, k;
int a[N], b[N];
int dt[N];
bool check(int mid){
int c = k, now = mid, y = -1;
for(int i = 1 ; i <= n ; i ++){
if(y != -1){
if(y >= a[i]){
y -= a[i];
continue;
}
else{
if(c == 0) return false;
c --, y = mid;
if(y < a[i]) return false;
y -= a[i];
continue;
}
}
if(now >= a[i]){
now -= a[i], now += b[i];
if(now > mid) now = mid;
}
else if(c){
c --, y = mid;
if(y >= a[i]) y -= a[i];
else return false;
}
else return false;
}
return true;
}
bool END;
signed main(){
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
#ifdef StreamSync
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
#endif
cin>>n>>k;
for(int i = 1 ; i <= n ; i ++){
cin>>a[i]>>b[i];
}
int l = 0, r = 1e18;
while(l < r){
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
cout<<l;
cerr<<"Time : "<<(db)clock() / CLOCKS_PER_SEC<<"s\n";
cerr<<"Memory : "<<(db)abs(&END - &START) / 1024.0 / 1024.0<<"MB\n";
return 0;
}

浙公网安备 33010602011771号