zoukankan      html  css  js  c++  java
  • 【比赛】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;
    }
    
  • 相关阅读:
    [转]Asp.Net页面输出到WORD、EXCEL、TXT、HTM等类型的文档
    人工智能AI基础 四
    关于设计的一点小结 四
    Visual Studio 11 将强化对2D/3D游戏开发的支持 四
    如何正确的对待设计模式 四
    C++设计模式原型模式 四
    C++游戏编程8步云 四
    Qt编译 四
    软件架构师应该知道的97件事 四
    给年轻程序员的几句话 四
  • 原文地址:https://www.cnblogs.com/hongyj/p/10206104.html
Copyright © 2011-2022 走看看