当前位置:网站首页>[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 ~
边栏推荐
猜你喜欢
随机推荐
latex如何打空格
新版图解网络PDF即将发布
There are four ways to write switch, you know
leetcode刷题:栈与队列05(逆波兰表达式求值)
安装mysql时出现:需要这两个包perl(Data::Dumper),perl(JSON)
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
杰理之、产线装配环节【篇】
C中main函数的几种写法
Case of camera opening by tour
分离字符串中的字母和数字并使得字母在前数组在后
News classification based on LSTM model
想请教一下,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
leetcode刷题:二叉树03(二叉树的后序遍历)
东哥套现,大佬隐退?
最近公共祖先(LCA)在线做法
中通笔试题:翻转字符串,例如abcd打印出dcba
ICML2022 | 基于元语义正则化的介入性对比学习
喜马拉雅自研网关架构演进过程
JS how to get a list of elements in a collection object
运放-滞回(迟滞)比较器全流程实战计算