当前位置:网站首页>[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
2022-07-01 21:32:00 【璇玑你没有心】
其实这两个题都是一样的思路,一个正着变,一个反着变。所以可以从前的到后遍历数组,如果a[i] > a[i - 1],就加上他们之间的差值,如果a[i] <= a[i - 1],a[i]可以借助a[i - 1]来实现,也就是不用单独再多出来次数了【贪心】。如果看差分的话,即答案为把差分数组所有正数加起来的和。
积木大赛
原题链接:登录—专业IT笔试面试备考平台_牛客网
AC代码:
#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;
}
道路铺设
AC代码:
一样的思路,可以自己写一下~
边栏推荐
猜你喜欢
随机推荐
薛定谔的日语学习小程序源码
从MLPerf谈起:如何引领AI加速器的下一波浪潮
Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
PWN攻防世界cgpwn2
Principle of motion capture system
php反射型xss,反射型XSS测试及修复
从20s优化到500ms,我用了这三招
打出三位数的所有水仙花数「建议收藏」
Halcon知识:三维重构的一个尝试
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
运放-滞回(迟滞)比较器全流程实战计算
杰理之烧录都使用 VBAT 供电,供电电压 4.2V【篇】
How to connect the two nodes of the flow chart
东哥套现,大佬隐退?
基础—io密集型计算和cpu密集型计算
vscode的使用
GCC编译
Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
同花顺股票开户选哪个券商好手机开户是安全么?
EMC-电路保护器件-防浪涌及冲击电流用