当前位置:网站首页>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();
}边栏推荐
- 小白学习MySQL - 统计的'投机取巧'
- [openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.
- Package for gbase 8s
- Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- UCLA | generative pre training for black box optimization
- OBS Browser+浏览器的基本使用
- [kubernetes series] installation and use of Helm
- 使用文本分析识别一段文本中的主要性别
- UCLA | 用于黑盒优化的生成式预训练
猜你喜欢

Musk released humanoid robot. Why is AI significant to musk?

cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。

CTF_ Web: basic 12 questions WP of attack and defense world novice zone

js的sort()函数

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

CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone

CTF_ Web: Learn flask template injection (SSTI) from 0

LabVIEW开发气体调节器

CTF_ Web: Advanced questions of attack and defense world expert zone WP (19-21)
![[esp32 learning path 6 - Flash encryption]](/img/4c/f317ca4823dca50a9bccd285967ab0.png)
[esp32 learning path 6 - Flash encryption]
随机推荐
Laravel document sorting 2. Route related
Finereport (sail soft) handling the problem that the histogram data label is blocked
Lecture record: history and development of strapdown inertial navigation solution
Basic use of OBS browser+ browser
Basic introduction of gbase 8s blocking technology
什么是数据持久化?
GBase 8s 锁的分类
什么是持久化?redis 持久化中的RDB和AOF是什么?
CTF_ Web: deserialization learning notes (I) classes and objects in PHP
GBASE 8s的并行操作问题场景描述
CTF_ Web:8-bit controllable character getshell
Data view for gbase 8s
SQL注入详解
GBASE 8s的数据视图
Win10 environment phpstudy2016 startup failure record
Coinlist queuing tutorial to improve the winning rate
Doubts about judging the tinyint field type of MySQL
Xiaobai learns MySQL - Statistical 'opportunism'
GbASE 8s中的Blob 页(Blobspace page)
English Grammar - pronunciation rules