codeforces 496A. Minimum Difficulty 解题报告

时间:2024-09-27 15:33:08

题目链接:http://codeforces.com/contest/496/problem/A

题目意思:给出有 n 个数的序列,然后通过删除除了第一个数和最后一个数的任意一个位置的数,求出删除这个数之后序列的最大相邻差是多少,然后删除下一个数,继续求出最大相邻差,直到删到倒数第二个数,最后从这些最大相邻差中找出最小的那个输出。例如:有序列为1 2 3 7 8,删除第二个、第三个、第四个数后得到的序列分别为:(1, 3, 7, 8), (1, 2, 7, 8), (1, 2, 3, 8)。那么最大相邻差分别为 4,5,5,选出最小的那个就是答案 4 。

  是由于bc不会做,临时走来做 virtual 的,效果当然不是太好。。。

  可以这样想,两重循环(外循环 i ,内循环j),i 表示当前需要删除第 i 个数,j 用来删除第 i 个数之后的序列中,最大相邻差。一种很简单的办法是,

if 【i == j】   d = a[j+1] - a[i-1]

  else     d = a[j+1] - a[j]

  else 语句用得比较巧妙,例如对于 1 2 3 7 8 这个序列,如果当前删除的是3,那么序列就变成 1 2 7 8。当算到 7 这个数的时候, d = 7 - 3,虽然这个 d 并不存在(3 没有了嘛),但是算了根本不会影响结果,因为 7 - 3 绝对比 7 - 2 小!

  

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
const int INF = + ;
int a[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
int minans = INF;
for (int i = ; i <= n-; i++)
{
int maxans = -INF;
for (int j = ; j <= n-; j++)
{
if (i == j)
maxans = max(maxans, a[j+] - a[i-]);
else
maxans = max(maxans, a[j+] - a[j]);
}
minans = min(minans, maxans);
}
printf("%d\n", minans);
}
return ;
}