当前位置:网站首页>B. Mark the Dust Sweeper(思维)
B. Mark the Dust Sweeper(思维)
2022-07-19 14:15:00 【罗gkv】
题意
给定n个数,可以执行以下操作
选择下标i<j,且a[i],a[i+1],…a[j-1]都大于0;令a[i]=a[i]-1,a[j]=a[j]+1。
问最少需要多少次上述操作,才能将a[1],a[2],…,a[n-1]都置为0。
思路
要让a[1],a[2],…,a[n-1]都置为0,需要将它们都搬运到a[n]上,因此答案为a[1]+a[2]+…+a[n-1]。又由于搬运过程,要求中途的元素都大于0,因此,遇到0值时,且它前面还有非0元素,则说明需要至少填充一个元素到该位置。
补完桥,才能过河。
结合代码理解。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 200010;
const int mod = 1e9 + 7;
int n;
int a[maxn];
void solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
ll res = 0;
bool all_zero = true;
for (int i = 0; i < n - 1; ++i) {
res += a[i];
if (!a[i]) {
if (!all_zero) {
++res;
}
} else {
all_zero = false;
}
}
printf("%lld\n", res);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
边栏推荐
猜你喜欢
随机推荐
跨机器的文件传输
第三十二基础:页面元素定位方式xpath----轴定位方式
短视频直播系统源码
算法---翻转数位(Kotlin)
VScode安装Eide,用于stm32开发
Feature Engineering -- numerical feature normalization
How to create a 3D world? Explore with pictures
TiDB数据库
[200 opencv routines] 237 Direction correction based on principal component extraction (openCV)
【毕业设计】基于微服务框架的电影院订票管理系统
Emeditor based corpus statistics and analysis
小程序毕设作品之微信小程序点餐系统毕业设计(2)小程序功能
小程序毕设作品之微信小程序点餐系统毕业设计(5)任务书
The general plan video is open! The first special account product of Quanguo fund will be launched soon, with 28 year old asset management veteran wangguobin at the helm!
kylin开启dashboard监控面板
Twelve kinds of wealth in life
Taishan Office Technology Lecture: Taking A4 as an example, what is the page structure
归并排序及优化
Pager类
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎