当前位置:网站首页>CF [1700d] D. River locks (DP, bisection, Mathematics)
CF [1700d] D. River locks (DP, bisection, Mathematics)
2022-06-23 05:30:00 【Elucidation】
Improved the details .
The question :
- A large reservoir consists of n A volume of v i vi vi Liters of water tank , Each water tank has a nozzle , Add one liter of water per second after opening . If the current tank i Full of , The extra water will be left supernaturally until i + 1 In the water tank , Until finally left to the sea .
- q Time to ask , One time at a time t j tj tj , Ask to fill up in a given time n What is the minimum number of water inlets to be opened for each water tank , If the output cannot be filled -1.
Ideas :
- obviously First step greedy , If you want to drive x A water outlet is opened in [1,x] , Drive ahead , After overflowing, the water can be replenished to the rear water tank , It takes less time to fill .
- The second step It is easy to deviate . In fact, two points will think , But I don't know how to do it . Here we should first derive a formula :
- Assume that the total reservoir volume is known S, The given time limit is T; Within this time limit , The minimum number of nozzles to be opened has actually been determined !, Number of open nozzles c n t > = S / T cnt >= S/T cnt>=S/T ( Round up ), Satisfy the minimum, so it is equal to ;
- cnt To determine the , Before, greed also knew that it was in front cnt individual , What remains to be determined is Before filling i Minimum time for one tank .
- therefore The third step , Set up dp state , Transfer , Plus binary optimization , The problem is solved .
1900 Gold content of
Let's look at the notes
C o d e : Code: Code:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define all(a) a.begin(),a.end()
#define sz(a) (int)a.size()
#define oper(a) operator<(const a& ee)const
#define endl '\n'
#define ul (u << 1)
#define ur (u << 1 | 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5 + 10, M = 2e5 + 10, mod = 1e8;
int INF = 0x3f3f3f3f; ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, k;
// Obviously greedy , The nozzle shall be opened in front as far as possible
int v[N], dp[N];// Before opening i Water inlet , Before filling at the same time i The earliest time of a water tank
ll sum[N];// Water volume prefix and
void work() {
dp[1] = v[1], sum[1] = v[1];
for (int i = 2; i <= n; i++) {
sum[i] = sum[i - 1] + v[i];
//dp[i] The earliest time is at least dp[i-1], Fill in the front to cover the back
int l = dp[i - 1], r = 1e9;
while (l < r)
{
int mid = l + r >> 1;
//mid Is time ,i Is the number of nozzles
if (1ll * mid * i < sum[i])l = mid + 1;
else r = mid;
}
dp[i] = l;
}
}
void solve() {
cin >> n;
forr(i, 1, n)cin >> v[i];
work();
// Clearly, what we require is the minimum number of outlets to fill the whole reservoir within a limited time , Why? dp This one is maintained ?
// Because the total amount of the reservoir is known S, Given the time limit T;
// Number of open nozzles cnt >= S/T ( Round up ), Satisfy the minimum, so it is equal to ;
//
// Meaning for : After this open number is satisfied , The reservoir will certainly fill up , But the time is unknown ,
// Because of a single vi The limitation of .
//
// After knowing the number of nozzles to be opened , We can substitute directly into dp Find the earliest time in , Just compare
cin >> m;
while (m--)
{
int tim; cin >> tim;
ll g = (sum[n] + tim - 1) / tim;
if (g <= n && dp[g] <= tim)cout << g << endl;
else cout << -1 << endl;
}
}
int main() {
cinios;
int T = 1;
for (int t = 1; t <= T; t++)
solve();
return 0;
}
/* */
边栏推荐
猜你喜欢

Missing essential plugin

Qt QWidget嵌套相对位置获取 (qt 画线 嵌套)

Xa mode explanation and code implementation of four Seata modes

物联网开源开发平台 Shifu 开放内测!第一版技术文档发布

VMware network connection error unit network service not found

MCS: continuous random variable chi square distribution

Win软件 - (Net-Framework)已处理证书链,但是在不受信任提供程序信任的根证书中终止

【opencv450】 图像相减、二值化、阈值分割

MCS:连续随机变量——Chi-Square分布

gis利器之Gdal(三)gdb数据读取
随机推荐
Chapter IX app project test (1)
The weak are as irritable as tigers, the strong are as calm as water, and the really powerful have already given up their emotions
What does it mean to open more accounts? Why open more accounts? How to implement it safely?
云原生数据库是未来数据库的天下
Event日志关键字:EventLogTags.logtags
Master shell, one article is enough!
QT QWidget nesting relative position acquisition (QT drawing nesting)
牛B程序员在“创建索引”时都会注意啥?
618 how to break through the siege? Haier Zhijia: do a good job in digitalization of users
C language stack implementation
WebRTC[47] - WebRTC 保存 YUV 数据的常用方式
Jetpack compose menubar Desktop Menu from door opening to entry
架构师之路,从「存储选型」起步
MCS:连续随机变量——Student’s t分布
Spark 离线开发框架设计与实现
Introduction to MySQL (II) sub query + Association
JDBC入门学习(三)之事务回滚功能的实现
99 multiplication table bat
bat中获取bat命令结果
pygame音乐相关的功能实现