luogu填坑

我一开始的思路:从1开始遍历,比如样例中,4>0那么开始往她后面寻找不等于0的道路一起-1,计为一次cnt+1

复制代码
#include<stdio.h>
int main(){
	int n;
	int str[10008];
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		scanf("%d",&str[i]);
	}
	int cnt=0;
	for(int i=0;i<n;i++){
		while(str[i]!=0){
			for(int j=i;j<n;j++){
				if(str[j]==0) break;
				str[j]-=1;
			}
			cnt++;
		}
	}
	printf("%d",cnt);
}

但是时间超限了

于是就有了第二个思路:

复制代码
#include<stdio.h>
#define ll long long
	int str[100008];
int main(){
	int n;
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		scanf("%d",&str[i]);
	}
	ll cnt=0;
	for(int i=1;i<n;i++){
		if(str[i]>str[i-1]){
			cnt+=str[i]-str[i-1];
		}
	}
	printf("%lld",cnt+str[0]);
}
相关推荐
倒头就睡的小比特2 天前
算法竞赛C++常用的STL
c++·算法
weilx12342 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼2 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛2 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光2 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark2 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·2 天前
C++ 萌新语法入门篇
c++·算法比赛