模板训练_1

今天某个w去打ACM了,然而垃圾的我看到T就已经不会放弃了..

于是刷了几道历年noip的水体&&模板题练习打代码速度

P1969 积木大赛

2017-09-15

首先是一个贪心

P1969 积木大赛
题目描述
春春幼儿园举办了一年一度的“积木大赛”。今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi。
在搭建开始之前,没有任何积木(可以看成n块高度为 0 的积木)。接下来每次操作,小朋友们可以选择一段连续区间[l, r],然后将第第 L 块到第 R 块之间(含第 L 块和第 R 块)所有积木的高度分别增加1。
小 M 是个聪明的小朋友,她很快想出了建造大厦的最佳策略,使得建造所需的操作次数最少。但她不是一个勤于动手的孩子,所以想请你帮忙实现这个策略,并求出最少的操作次数。
输入输出格式
输入格式:
输入文件为 block.in
输入包含两行,第一行包含一个整数n,表示大厦的宽度。
第二行包含n个整数,第i个整数为hi 。
输出格式:
仅一行,即建造所需的最少操作数。
输入输出样例
输入样例#15
2 3 4 1 2
输出样例#15
【样例解释】

其中一种可行的最佳方案,依次选择

[1,5] [1,3] [2,3] [3,3] [5,5]

【数据范围】

对于 30%的数据,有1 ≤ n ≤ 10;

对于 70%的数据,有1 ≤ n ≤ 1000;

对于 100%的数据,有1 ≤ n ≤ 1000000 ≤ hi≤ 10000
T1

题解

只有上一个对当前有贡献,贪心

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define ll long long
#include<queue>
using namespace std;
const int maxn=100000+999;
int read(){
    int an=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while('0'<=ch&&ch<='9'){an=an*10+ch-'0';ch=getchar();}
    return an*f;
}
ll ans;
int n;
ll hi[maxn];
int main(){
    n=read();
    for(int i=1;i<=n;i++)hi[i]=read();
    for(int i=1;i<=n;i++){
        if(hi[i]-hi[i-1]>0)
        ans+=hi[i]-hi[i-1];
    }
    cout<<ans;
    return 0;
}
贪心

P1339 [USACO09OCT]热浪Heat Wave

裸单元最短路

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define ll long long
#include<queue>
#define _ =read();
using namespace std;
const int maxn=6200+999;
const int INT=2147483647;
int read(){
    int an=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while('0'<=ch&&ch<='9'){an=an*10+ch-'0';ch=getchar();}
    return an*f;
}
int f[maxn];
int Rs,Re,n,m,cnt,spf[maxn],ans;
bool vis[maxn];
queue<int>q;
struct saber{
int nex,to,wi;
}b[maxn<<1];
void add(int x,int y,int z){
    cnt++;
    b[cnt].to=y;
    b[cnt].wi=z;
    b[cnt].nex=f[x];
    f[x]=cnt;
}
void spfa(){
    q.push(Rs);
    vis[Rs]=1;spf[Rs]=0;
    while(!q.empty()){
        int x=q.front();q.pop();vis[x]=0;
        for(int i=f[x];i;i=b[i].nex){
            int v=b[i].to;
                if(spf[v]>spf[x]+b[i].wi){
                    spf[v]=spf[x]+b[i].wi;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    ans=spf[Re];
}
int main(){
    n _ m _ Rs _ Re _
    for(int i=1;i<=m;i++){
        int x,y,z;
        x _ y _ z _ 
        add(x,y,z);add(y,x,z);
    }
    for(int i=1;i<=n;i++)spf[i]=INT; 
    spfa();
    cout<<ans;
    return 0;
}
spfa
原文地址:https://www.cnblogs.com/ck666/p/7525063.html