51 Nod 1402 最大值

1402 最大值 

题目来源: TopCoder

基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题

 收藏

 关注

一个N长的数组s[](注意这里的数组初始下标设为1,而不是0,即N个元素为s[1],s[2],...,s[N]),满足以下性质:
1)每个元素都是非负的整数,且s[1]=0;
2)任意两个相邻元素差值的绝对值不大于1,即| s[i]-s[i+1] |<=1;
3)对于部分特殊点xi,要求s[xi]<=ti(这样的特殊点一共M个);
问在以上约束下s[]中的最大值最大可能是多少?

Input

多组测试数据,第一行一个整数T,表示测试数据数量,1<=T<=5
每组测试数据有相同的结构构成:
第一行两个整数N,M,表示s[]的长度与特殊点的个数,其中1<=N<=100000,0<=M<=50.
之后M行,每行两个整数xi与ti,其中1<=xi<=N,0<=ti<=100000,且xi以增序给出。

Output

每组数据一行输出,即数组的可能最大值。

Input示例

3
10 2
3 1
8 1
100000 0
2718 5
1 100000
30 100000
400 100000
1300 100000
2500 100000

Output示例

3
99999
2717

程序设计真是门艺术,用这种方法实现思维难度瞬间下降了好多好多。

感谢这位博主的文章:https://www.cnblogs.com/MasterSpark/p/7625503.html

ac代码:


#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int t;
int n,m;
int h1[100005];
int h2[100005];
int MAX[100005];
int main()
{
#ifndef ONLINE_JUDGE
   freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(MAX,inf,sizeof(MAX));
        int x,h;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&h);
            MAX[x]=h;
        }
        h1[1]=0;int maxh1=0;
        for(int i=2;i<=n;i++)
        {
            maxh1++;
            h1[i]=min(MAX[i],maxh1);
            maxh1=h1[i];
        }
        int maxh2=99999999;
        for(int i=n;i>=1;i--)
        {
            maxh2++;
            h2[i]=min(MAX[i],maxh2);
            maxh2=h2[i];
        }
        int ans=-1;
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,min(h1[i],h2[i]));
        }
        printf("%d
",ans);
    }
    return 0;
}




原文地址:https://www.cnblogs.com/linruier/p/9768991.html