• 【比赛】NOIP2018 铺设道路


    原题,而且还是CCF自己的

    考虑对于一段最长不上升序列,无论如何都至少有序列第一个数的贡献,可以知道,这个贡献是可以做到且最少的

    然后对于序列最后一位,也就是最小的那一个数,可以和后面序列拼起来的就拼起来,所以后面的序列需要补偿的贡献就是差分

    简化一下, (ans=sum_{i=1}^nmax{0,(a_i-a_{i-1})})

    #include<bits/stdc++.h>
    #define ui unsigned int
    #define ll long long
    #define db double
    #define ld long double
    #define ull unsigned long long
    #define ft first
    #define sd second
    #define pb(a) push_back(a)
    #define mp(a,b) std::make_pair(a,b)
    #define REP(a,b,c) for(register int a=(b),a##end=(c);a<=a##end;++a)
    #define DEP(a,b,c) for(register int a=(b),a##end=(c);a>=a##end;--a)
    const int MAXN=100000+10;
    int h[MAXN],n;
    ll ans;
    template<typename T> inline void read(T &x)
    {
    	T data=0,w=1;
    	char ch=0;
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-')w=-1,ch=getchar();
    	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    	x=data*w;
    }
    template<typename T> inline void write(T x,char ch='')
    {
    	if(x<0)putchar('-'),x=-x;
    	if(x>9)write(x/10);
    	putchar(x%10+'0');
    	if(ch!='')putchar(ch);
    }
    template<typename T> inline bool chkmin(T &x,T y){return y<x?(x=y,true):false;}
    template<typename T> inline bool chkmax(T &x,T y){return y>x?(x=y,true):false;}
    template<typename T> inline T min(T x,T y){return x<y?x:y;}
    template<typename T> inline T max(T x,T y){return x>y?x:y;}
    int main()
    {
    	freopen("road.in","r",stdin);
    	freopen("road.out","w",stdout);
    	read(n);
    	REP(i,1,n)read(h[i]),ans+=max(0,h[i]-h[i-1]);
    	write(ans,'
    ');
    	return 0;
    }
    
  • 相关阅读:
    gitio博客搭建,hexo + NeXT
    [MIsc]JD笔试编程题
    [MATH]Big Integer +
    【Math】GCD XOR 证明
    【Math】最近点对
    【SRM】600#div2 B 枚举
    【Game】组合游戏
    【Game】找出游戏必胜态
    【DP】树形DP 记忆化搜索
    141. Linked List Cycle
  • 原文地址:https://www.cnblogs.com/hongyj/p/10206104.html
Copyright © 2020-2023  润新知