$Noip2018/Luogu5019/Luogu1969$ 铺设道路
去年$Noip$的时候我并没有做过原题,然后考场上也没有想出正解,就写了个优化了一点的暴力:树状数组+差分,然后就$A$了$ovo$.
$Sol$
只要$O(N)$扫一遍,只要当前值比前一个值大,那么答案就累计这两个值的差的绝对值.$over.$
$Code$
#include<iostream>
#include<cstdio>
#define rg register
#define ll long long
using namespace std;
int read()
{
int x=,y=;char c;
c=getchar();
while(c<''||c>'') {if(c=='-') y=-;c=getchar();}
while(c>=''&&c<='') {x=(x<<)+(x<<)+c-'';c=getchar();}
return x*y;
}
int n;
ll ans;
int a[],b[];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int k)
{
for(rg int i=x;i<=n;i+=lowbit(i)) b[i]+=k;
}
ll sum(int x)
{
ll sum1=;
for(rg int i=x;i>=;i-=lowbit(i))
sum1+=b[i];
return sum1;
}
int minn,l,r;
int main()
{
freopen("road.in","r",stdin);
freopen("road.out","w",stdout);
n=read();minn=;
for(rg int i=;i<=n;i++)
{
a[i]=read();
minn=min(a[i],minn);
add(i,a[i]-a[i-]);
}
ans+=minn;
add(,-minn);
if(a[]!=) l=;
for(rg int i=;i<=n;i++)
{
int s=sum(i);
if(s)
{
if(l==) {l=i;r=i;minn=s;}
else {minn=min(minn,s);r=i;}
if(i!=n) continue ;
}
else if(l==) continue;
ans+=minn;
add(l,-minn);
if(r<n) add(r+,minn);
i=l-;l=;
}
printf("%lld",ans);
return ;
}
View 考场 Code
#include<iostream>
#include<cstdio>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;++i)
#define ll long long
using namespace std;
il int read()
{
Rg int x=,y=;char c=getchar();
while(c<''||c>''){if(c=='-')y=-;c=getchar();}
while(c>=''&&c<=''){x=(x<<)+(x<<)+c-'';c=getchar();}
return x*y;
}
int n,x,y;ll as;
int main()
{
n=read();
go(i,,n){y=read();if(y>x)as+=y-x;x=y;}
printf("%lld\n",as);
return ;
}
View 正解 Code
随机推荐
- MapReduce数据流-Mapper
- php服务端允许跨域访问
>>php服务端允许跨域访问<< >>同源策略和跨域解决方案<<
- @noi - 172@ 追捕大象
目录 @description@ @solution@ @accepted code@ @details@ @description@ 在一块平原上有一头大象. 平原被分成 n×m 个格子.初始时大象 ...
- 利用scrapy爬取文件后并基于管道化的持久化存储
我们在pycharm上爬取 首先我们可以在本文件打开命令框或在Terminal下创建 scrapy startproject xiaohuaPro ------------创建文件 scrapy ...
- Project Euler Problem 18-Maximum path sum I & 67-Maximum path sum II
基础的动态规划...数塔取数问题. 状态转移方程: dp[i][j] = num[i][j] + max(dp[i+1][j],dp[i+1][j+1]);
- SuperSocket性能数据采集的应用程序接口的改动
性能数据采集的应用程序接口作了修改,两个虚方法已经被更改: protected virtual void UpdateServerSummary(ServerSummary serverSummary ...
- 2019-8-31-jekyll-在博客添加流程图
title author date CreateTime categories jekyll 在博客添加流程图 lindexi 2019-08-31 16:55:59 +0800 2018-2-13 ...
- HOSt ip is not allowed to connect to this MySql server, MYSQL添加远程用户或允许远程访问三种方法
HOSt ip is not allowed to connect to this MySql server 报错:1130-host ... is not allowed to connect to ...
- springmvc url处理映射的三种方式:
一.SpringMVC简介 SpringMVC是一种基于Spring实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,使用了MVC架构模式的思想,将web层进行职责解耦,并管理应用所需对象 ...
- jQuery中动态创建、添加元素的方法总结
<input type="button" value="创建元素" id="btn"> <div id="box ...