51nod 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

随手那么一敲就过了- -


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <algorithm>
using namespace std;
int s[100005];
struct yu
{
    int x,y;
}d[100005];
bool cmp(yu a, yu b)
{
    return a.x<b.x;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(s,0,sizeof(s));
        memset(d,0,sizeof(d));
        int n,m;
        cin>>n>>m;
        for(int i=0;i<m;i++)
        {
            cin>>d[i].x>>d[i].y;
        }
        int j=2;
        int y;
        int maxs=0;
        for(int i=0;i<m;i++)
        {
            y=j;
            for(;j<=d[i].x;j++) //正着使值最大
            {
                s[j]=s[j-1]+1;
            }
            s[d[i].x]=min(s[d[i].x],d[i].y);
            for(int k=d[i].x-1;k>=1;k--)//这里错了一次,因为更新的问题,每次要更新所有的数组。比如前面的大,但是后面的值很小,这样就要倒着更新前面的值
            {
                s[k]=min(s[k],s[k+1]+1);//倒着使最小里面找最大。s[k+1]+1是尽量让值大
            }
            j=d[i].x+1;
        }
        for(;j<=n;j++)
        {
            s[j]=s[j-1]+1;
        }
        for(j=1;j<=n;j++)
        {
            if(s[j]>maxs) maxs=s[j];//最后的成品数组里面找最大的
        }

       // for(int i=0;i<=n;i++)cout<<i<<' '<<s[i]<<' '<<endl;;cout<<endl;
        cout<<maxs<<endl;
    }
}




附赠测试数据 


答案为

8
120
6343




3
20 5
4 3
7 8
13 1
15 17
18 16
447 27
32 81
35 221
55 172
60 641
61 25
88 953
91 330
97 141
128 123
151 440
181 692
186 394
192 200
196 649
198 78
237 726
259 50
268 810
291 501
314 4
341 216
367 407
389 2
390 172
391 0
428 29
435 14
97638 42
8 16610
1749 6
4550 497
5388 14
6752 42892
7089 31
9737 79
14891 1203
16671 518
16821 31147
17691 597
19753 7846
24589 1396
25348 8309
30114 12
32213 14
34376 1148
36467 433
37699 23693
41490 13
44784 1939
44893 244
57316 19
58275 46
58567 27
61122 611
61489 412
63195 10
64776 27023
65905 19141
68788 34
69908 15667
72853 588
78152 10
78784 229
82779 83
84488 390
86277 14
88611 38441
92793 16021
93214 4
97583 39386





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值