您的位置:首页 > 其它

51nod 1402最大值

2016-04-15 10:37 633 查看
一个N长的数组s[](注意这里的数组初始下标设为1,而不是0,即N个元素为s[1],s[2],...,s
),满足以下性质:

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: