当前位置:网站首页>Codeforces Round #802 (Div. 2)
Codeforces Round #802 (Div. 2)
2022-06-27 05:17:00 【nth2000】
C topic -Helping the Nature


Ideas
- Direct thinking : Make each adjacent tree the same height . It can only be done for each adjacent tree , Process one of the prefixes or suffixes according to size , The same height is the smaller of the two , And none of these processing sequences can be less .
- Idea of difference ( turn ):
among d 1 = a [ 1 ] − a [ 0 ] d_1 = a[1] - a[0] d1=a[1]−a[0], among a [ 0 ] = 0 a[0]=0 a[0]=0
When I see the interval operation, I think of prefix and difference . It belongs to the common routine
#include <bits/stdc++.h>
using namespace std;
#include<stack>
#define int long long
signed main()
{
int t;
cin >> t;
for(int i = 0;i<t;i++)
{
int n;
cin >> n;
int a[n];
for(int k = 0;k<n;k++) cin>>a[k];
int ans = 0;
int temp = a[n - 1];
for(int k = n - 1;k>=1;k--)
{
if(a[k] - a[k - 1] >= 0) // The suffix is subtracted to make it equal to the prefix
{
ans += (a[k] - a[k - 1]);
temp -= (a[k] - a[k - 1]);
}
else
{
ans += (a[k - 1] - a[k]);
}
}
cout << ans + abs(temp) << endl;
}
system("pause");
}
D topic -Helping The Nature



Ideas
- The necessary condition is to reduce the search space
- Find out if it can meet DP
set up D P [ i ] DP[i] DP[i], Deal with the second i individual lock, front i individual lock All on , And fill it up i individual lock The shortest time needed .guess:
- Ruodi i individual lock Can be in front i-1 individual lock Before or just before the time of filling , be D P [ i ] = D P [ i − 1 ] DP[i] = DP[i-1] DP[i]=DP[i−1]
- otherwise , front i-1 individual lock Spilled water and the i individual lock The water in the pipes will mix with each other . It can be regarded as the former i Two pipes forward i individual lock Water injection . Time required ⌈ s u m ( v [ 1 : i ] ) i ⌉ \lceil \frac{sum(v[1:i])}{i}\rceil ⌈isum(v[1:i])⌉
therefore D P [ i ] = m a x ( D P [ i − 1 ] , ⌈ s u m ( v [ 1 : i ] ) i ⌉ ) DP[i] = max(DP[i-1],\lceil \frac{sum(v[1:i])}{i}\rceil) DP[i]=max(DP[i−1],⌈isum(v[1:i])⌉)
For each query, The necessary condition is t q u e r y > = d p [ n ] t_{query} >= dp[n] tquery>=dp[n]. In such a period of time , If take q A pipe , It is guaranteed that there will be sufficient time to q One full of .
- If before opening q There are two pipes d p [ n ] dp[n] dp[n] All the reservoirs have been filled up within the time , be q The conditions must be satisfied . And there must be t q u e r y ⋅ q ≥ d p [ n ] ⋅ q ≥ s u m ( v [ 1 : n ] ) t_{query} \cdot q \geq dp[n] \cdot q\geq sum(v[1:n]) tquery⋅q≥dp[n]⋅q≥sum(v[1:n])
- Otherwise, it can be regarded as before q The pipeline is filled with water at the same time , Rate mixing . Therefore, it is necessary to ensure that t q u e r y t_{query} tquery In time , With q rate , Can fill the reservoir , Also have : t q u e r y ⋅ q ≥ s u m ( v [ 1 : n ] ) t_{query} \cdot q \geq sum(v[1:n]) tquery⋅q≥sum(v[1:n])
So given q, Just checking
t q u e r y ⋅ q ≥ s u m ( v [ 1 : n ] ) t_{query} \cdot q \geq sum(v[1:n]) tquery⋅q≥sum(v[1:n]) Whether it is satisfied or not is enough . Find the smallest one q that will do .
#include <bits/stdc++.h>
using namespace std;
#include<stack>
#define int long long
signed main()
{
int n;
cin >> n;
int v[n];
int prev[n + 1];
prev[0] = 0;
for(int i = 0;i<n;i++)
{
scanf("%ld",&v[i]);
prev[i + 1] = prev[i] + v[i];
}
int dp[n + 1];
dp[1] = v[0];
for(int i = 2;i<=n;i++) dp[i] = max(dp[i - 1],(long long)ceil((double)prev[i] / i));
int q;
cin >> q;
for(int i = 0;i<q;i++)
{
int t;
scanf("%ld",&t);
if(t < dp[n]) printf("-1\n");
else // Find the first one greater than or equal to ceil(prev[n]/t) Of prefixsum The sum of the
{
t = (int)ceil((double)prev[n]/t);
printf("%ld\n",t);
}
}
system("pause");
}
边栏推荐
- Flink production problems (1.10)
- [station B up dr_can learning notes] Kalman filter 2
- OpenCV的轮廓检测和阈值处理综合运用
- Halon common affine transformation operators
- Microservice system design -- Distributed timing service design
- Microservice system design -- distributed lock service design
- C语言实现定时器
- Flink生产问题(1.10)
- 体验 win10 下 oceanbase 数据库
- Redis高可用集群(哨兵、集群)
猜你喜欢

EasyExcel合并相同内容单元格及动态标题功能的实现

体验 win10 下 oceanbase 数据库

AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法

流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)

Microservice system design -- microservice monitoring and system resource monitoring design
![[nips 2017] pointnet++: deep feature learning of point set in metric space](/img/3e/0a47eecc27f236d629c611e683b37a.png)
[nips 2017] pointnet++: deep feature learning of point set in metric space

RTP 发送PS流工具(已经开源)

leetcode299周赛记录

STM32 reads IO high and low level status

双位置继电器DLS-34A DC0.5A 220VDC
随机推荐
019 basics of C language: C preprocessing
Microservice system design -- Distributed timing service design
pycharm 如何安装 package
019 C语言基础:C预处理
Neo4j database export
neo4j数据库导出
使用域名转发mqtt协议,避坑指南
017 basics of C language: bit field and typedef
The most detailed download tutorial of MySQL
Almost because of json Stringify lost his bonus
C语言实现定时器
双位置继电器HJWS-9440
021 basics of C language: recursion, variable parameters
认知篇----2022高考志愿该如何填报
微服务系统设计——统一鉴权服务设计
Remapping (STM32)
leetcode-20. Valid parentheses -js version
微服务系统设计——分布式锁服务设计
微服务系统设计——分布式事务服务设计
双位置继电器DLS-34A DC0.5A 220VDC