51nod 1402最大值

一个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 <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define esp  1e-8
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int inf = 2147483647;
const long long mod = 1000000007;
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
int x[55], t[55];
int main()
{
	int T, n, m, i, j;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &n, &m);
		for (i = 1; i <= m; ++i)
		{
			scanf("%d%d", &x[i], &t[i]);
		}
		int ans = 0;
		x[0] = 1;
		t[0] = 0;
		t[m + 1] = 999999999;
		x[m + 1] = n;
		for (i = 1; i <= m; ++i)
		{
			if (x[i] == 1)
			{
				t[i] = 0;
				continue;
			}
			t[i] = min(t[i], x[i] - x[i - 1] + t[i - 1]);
		}
		for (i = m; i >= 0; --i)
		{
			t[i] = min(t[i], x[i + 1] - x[i] + t[i + 1]);
		}
		for (i = 1; i <= m; ++i)
		{
			int a = (t[i] + x[i] - x[i - 1] - t[i - 1]) / 2;
			a = min(a, x[i] - x[i - 1]);
			ans = max(ans, t[i - 1] + a);
		}
		ans = max(ans, t[m] + n - x[m]);
		printf("%d\n", ans);
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值