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\n",ans);
}
return 0;
}

最新文章

  1. ASP.NET获取客户端IP地址
  2. iOS打包ipa给客户测试流程
  3. JavaWeb 学习009-4个页面,5条sql语句(添加、查看、修改、删除)
  4. 转发 win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案
  5. UML分析与设计
  6. 可滑动的ToggleButton(开关)
  7. NOSQL的应用,Redis/Mongo
  8. Advanced Configuration Tricks
  9. mysql 权限分配及创建新用户
  10. Struts2实现单文件上传
  11. structs 拦截器
  12. JavaScript(15)jQuery 选择器
  13. poj 2378 树型dp
  14. js判断flash文件是否加载完毕
  15. 南理第八届校赛同步赛-C count_prime//容斥原理
  16. Nginx详解十一:Nginx场景实践篇之Nginx缓存
  17. re:从零开始的数位dp
  18. oracle 11 g release 2 卸载
  19. Driver 01 进程隐藏
  20. tomcat及nginx相关,格式化输出,配置及日志解析

热门文章

  1. python-day6(正式学习)
  2. mysql之存储过程基础
  3. 怎样理解HTMLCollection接口
  4. O038、Shelve Instance 操作详解
  5. Linux学习笔记:7个ssh命令用法
  6. &lt;(* ̄▽ ̄*)/低碳生活管理系统
  7. Java注解【三、注解的分类】
  8. centos8下jdk13和tomcat9的安装
  9. 渗透测试平台Vulnreport介绍与使用
  10. 【转】Qt 资源图片删除后,错误 needed by `debug/qrc_image.cpp&#39;. Stop. 的终极解决办法