当前位置:网站首页>[noip2013] building block competition [noip2018] road laying greed / difference
[noip2013] building block competition [noip2018] road laying greed / difference
2022-07-01 21:45:00 【Xuanji, you have no heart】
In fact, these two questions are the same idea , A positive change , One changes in reverse . So you can traverse the array from front to back , If a[i] > a[i - 1], Just add the difference between them , If a[i] <= a[i - 1],a[i] Can use a[i - 1] To achieve , That is, there is no need to come out more times alone 【 greedy 】. If you look at the difference , That is, the answer is the sum of all positive numbers in the difference array .
Building blocks competition
Original link : Sign in — major IT Written interview preparation platform _ Cattle from
AC Code :
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define PII pair<int,int>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rrep(i, n) for(int i = n; i >= 1; ++i)
using namespace std;
const double pi = acos(-1.0);
const int N = 1e5 + 10;
int a[N], b[N];
int main()
{
int n;
scanf("%d", &n);
int res = 0;
rep(i, n) scanf("%d", &a[i]), b[i] = a[i] - a[i - 1];
for(int i = 1; i <= n; i++)
if(a[i] > a[i - 1]) res += a[i] - a[i - 1];
cout << res;
return 0;
}
Road laying
Sign in — major IT Written interview preparation platform _ Cattle from
AC Code :
The same way of thinking , You can write it yourself ~
边栏推荐
猜你喜欢
MySQL数据库驱动(JDBC Driver)jar包下载
K-means based user portrait clustering model
locust 系列入门
【Leetcode】最大连续1的个数
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
深度学习 常见的损失函数
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
leetcode刷题:栈与队列04(删除字符串中的所有相邻重复项)
Significance and measures of security encryption of industrial control equipment
GCC编译
随机推荐
leetcode刷题:栈与队列05(逆波兰表达式求值)
从20s优化到500ms,我用了这三招
leetcode刷题:二叉树02(二叉树的中序遍历)
以飞地园区为样本,看雨花与韶山如何奏响长株潭一体化发展高歌
基于YOLOv5的口罩佩戴检测方法
4. 对象映射 - Mapping.Mapstercover
分离字符串中的字母和数字并使得字母在前数组在后
BPR(贝叶斯个性化排序)
Accelera Systems Initiative是一个独立的非营利组织
leetcode刷题:二叉树01(二叉树的前序遍历)
C中main函数的几种写法
想请教一下,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
收藏:存储知识全面总结
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
PCB线路板塞孔工艺的那些事儿~
Customize the insertion of page labels and realize the initial search of similar address books
基于LSTM模型实现新闻分类
TOPS,处理器运算能力单位、每秒钟可进行一万亿次
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
There are four ways to write switch, you know