当前位置:网站首页>Codeforces Round #802 (Div. 2) C D
Codeforces Round #802 (Div. 2) C D
2022-06-25 04:00:00 【想吃蛋黄肉粽】
C题意:给你一个序列a,你可以使用以下三个操作:
操作一:对于区间[1,i] - 1
操作二:对于区间[i,n] - 1
操作三:对于区间[1,n] + 1
求最小操作次数使得a序列全为0。
C分析:观察操作的本质:
操作一:使得差分d[i+1]+1
操作二:使得差分d[i]-1
操作三:操作三
O(n)铺平之后(所有数字都一样了)再加上a序列的一个数字即可。
C代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int a[maxn],d[maxn];
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
d[i]=a[i]-a[i-1];
}
int cnt=0;
int a1=a[1];
for(int i=2;i<=n;i++)
{
cnt+=abs(d[i]);
if(d[i]<0)
{
a1+=d[i];
}
}
cnt+=abs(a1);
cout<<cnt<<endl;
}
signed main()
{
ios::sync_with_stdio(0);
int t=1;
cin>>t;
while(t--) solve();
}D题意:有n个水库,每个水库的容量为v[i],且有一个水管连接,水管打开后每秒会流入1升的水。注意每个水库还连接着下一个水库,当第i个水库满了之后,溢出的水会流到第i+1个水库中。
你需要回答q次查询,每次给出时间t,求至少打开多少个水管才能使得所有水库在t时间内被灌满。
D分析:设打开x个水管,那么使得打开的水管最优必须是前面x个。考虑二分check(x,t) 开x个管道能否t秒灌满,显然只能支持O(1)的判断,灌满x后面的水库所需的时间好算,但需要保证前面的水库灌满。预处理一下,设dp[i]表示开前i个管道并灌满前i个水库所需的时间,可以O(n)解决。
D代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int a[maxn],sum[maxn];
int dp[maxn],ot[maxn];//开前i个管道并灌满前i个水库所需的时间
int n;
int check(int x,int t)//开x个管道能否t秒灌满
{
int rem=max(sum[n]-sum[x]-ot[x],0ll);
int tt=(rem+x-1)/x;
if(tt+dp[x]<=t) return 1;
return 0;
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
for(int i=1;i<=n;i++)
{
int t=max((a[i]-dp[i-1]-ot[i-1]+i-1)/i,0ll);
dp[i]=dp[i-1]+t;
ot[i]=dp[i]*i-sum[i];
}
int q;
cin>>q;
while(q--)
{
int t;
cin>>t;
int l=1,r=n,ans=-1;
while(l<=r)
{
int m=l+r>>1;
if(check(m,t))
{
r=m-1;
ans=m;
}
else l=m+1;
}
cout<<ans<<endl;
}
}
signed main()
{
ios::sync_with_stdio(0);
int t=1;
// cin>>t;
while(t--) solve();
}边栏推荐
- Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)
- Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly
- LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- Summary of various problems encountered by cocos2d-x
- Basic use of OBS browser+ browser
- Finereport (sail soft) handling the problem that the histogram data label is blocked
- Data import and export for gbase 8s
- Multithreading structure of gbase 8s
- Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)
猜你喜欢

Lecture record: data processing methods and applications of various spatial geodetic techniques

微信小程序父子组件之间传值

马斯克发布人形机器人,AI对马斯克为什么意义重大?

js的sort()函数

Value transfer between parent and child components of wechat applet

Error 1062 is reported during MySQL insertion, but I do not have this field.

什么是存储引擎以及MySQL常见的三种数据库存储引擎

【esp32学习之路6——flash加密】

Finereport (sail soft) handling the problem that the histogram data label is blocked

CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
随机推荐
冰冰学习笔记:循环队列的实现
Use of deferred environment variable in gbase 8s
GBASE 8s的数据视图
GBASE 8s存储过程执行和删除
PHP extracts and analyzes table contents, and collects bidding information
关于TCP连接三次握手的详细总结
Laravel document sorting 10. Request life cycle
简单的恶意样本行文分析-入门篇
Nodejs connects to MySQL through heidisql, and ER appears_ BAD_ DB_ ERROR: Unknown database 'my_ db_ books'
GBASE 8s的触发器
GBASE 8s存储过程语法结构
@Requestbody solution get parameter is null
小白学习MySQL - 统计的'投机取巧'
Coinlist queuing tutorial to improve the winning rate
LabVIEW development gas regulator
GBASE 8s活锁、死锁问题的解决
GBASE 8s存儲過程語法結構
PostgreSQL数据库WAL——RM_HEAP_ID日志记录动作
GBASE 8s的级联删除功能
Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)