当前位置:网站首页>Codeforces Round #802 (Div. 2) C D
Codeforces Round #802 (Div. 2) C D
2022-06-25 04:43:00 【I would like to have egg yolk and meat dumplings】
Portal :
C. Helping the Nature
C The question : Give you a sequence a, You can use the following three operations :
Operation 1 : For interval [1,i] - 1
Operation two : For interval [i,n] - 1
Operation three : For interval [1,n] + 1
Find the minimum number of operations so that a The sequence is all 0.
C analysis : Observe the nature of the operation :
Operation 1 : Make the difference d[i+1]+1
Operation two : Make the difference d[i]-1
Operation three : Operation three
O(n) After paving ( All the numbers are the same ) Plus a Just one number of the sequence .
C Code :
#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 The question : Yes n A reservoir , The capacity of each reservoir is v[i], And there is a water pipe connection , After the water pipe is opened, it will flow into 1 Liters of water . Note that each reservoir is also connected to the next reservoir , When the first i When the reservoir is full , The overflowing water will flow to i+1 In a reservoir .
You need to answer q Queries , Give time each time t, At least how many water pipes can be opened to make all reservoirs in t Filled in time .
D analysis : Set on x A water pipe , Then the best way to open the water pipe must be the front x individual . Consider dichotomy check(x,t) open x Can a pipeline t Fill it up in seconds , Obviously, it can only support O(1) The judgment of the , Fill up x It is easy to calculate the time required for the reservoir behind , But it is necessary to ensure that the reservoir in front is full . Prepare for it , set up dp[i] It means before opening i Before the pipes are filled i The time required for a reservoir , Sure O(n) solve .
D Code :
#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];// Before opening i Before the pipes are filled i The time required for a reservoir
int n;
int check(int x,int t)// open x Can a pipeline t Fill it up in seconds
{
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();
}边栏推荐
- 在 .NET 6 中使用 dotnet format 格式化代码
- 重磅直播 | 相移法+多频外差之数学原理推导+实现
- Office macro virus bounce shell experiment
- How to apply for software
- 「 每日一练,快乐水题 」1108. IP 地址无效化
- 【FLink】access closed classloader classloader.check-leaked-classloader
- CTF_ Web: how to recognize and evaluate a regular expression
- 计算学生成绩等级(虚函数和多态)
- Data view for gbase 8s
- Multithreading structure of gbase 8s
猜你喜欢

A detailed summary of TCP connection triple handshake

Vscode 设置clang-format

Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.

js中的concat()

GBASE 8s 索引R树

Efficient NoSQL database service Amazon dynamodb experience sharing

记录小知识点

js的arguments

"Daily practice, happy water" 1108 IP address invalidation

【FLink】access closed classloader classloader.check-leaked-classloader
随机推荐
Immutable學習之路----告別傳統拷貝
「 每日一练,快乐水题 」1108. IP 地址无效化
Join() in JSZ
WPF 使用 MAUI 的自绘制逻辑
哪个编程语言实现hello world最烦琐?
高效的NoSQL数据库服务Amozon DynamoDB体验分享
CTF_ Web: basic 12 questions WP of attack and defense world novice zone
Wechat likes to pay attention to the solution of invalid automatic reply
绝了!自动点赞,我用 PyAutoGUI!
At the age of 30, I began to learn programming by myself. Is it still time for me to have difficulties at home?
OOP 向量加减(友元+拷贝构造)
PostgreSQL database Wal - RM_ HEAP_ ID logging action
GBASE 8s存储过程流程控制
为什么TCP握手刚刚好是3次呢?
Records of ros2/dds/qos/ topics
Efficient NoSQL database service Amazon dynamodb experience sharing
Simple text analysis of malicious samples - Introduction
A detailed summary of TCP connection triple handshake
GBASE 8s存储过程语法结构
Construction scheme of distributed websocket