http://acm.hdu.edu.cn/showproblem.php?pid=6551
这个题卡了大概有两个小时 我很不要脸的想把原因归结为昨天是训练的第一天没有状态 很撒比 真的很蠢。。。
解题思路:这个题不用注意12小时之后的时间啊 因为是个钟表 我们把时间统一格式化为12小时制就可以了啊 然后不断更新最小时间 最大时间 同时如果需要复现的这次时间比起始时间大/小 我们要及时更新st1,st2 具体内容看注释8
代码来自https://www.cnblogs.com/graytido/p/11185452.html#3181869155
注释是我自己加的
#include<algorithm>
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<ctime>
#include<stack>
#include<map>
#include<set>
#include<cstring>
using namespace std;
typedef long long ll;
int main()
{
int n, h, m, s;
int h1, m1, s1;
int ans = 0;
int suma = 12 * 3600;//12个小时的秒数
while (~scanf("%d", &n))
{
scanf("%d%d%d", &h, &m, &s);
h %= 12;//转为12小时制
int start = h * 3600 + m * 60 + s;//起始时间
ans = 0;//初始化ans
int ed1 = start, ed2 = start; //倒回去最接近0,顺下去最接近12
int st1 = 0, st2 = suma; //倒回去最接近起点,顺下去最接近起点
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &h1, &m1, &s1);
h1 %= 12;
int sum = h1 * 3600 + 60 * m1 + s1;//纯秒数
ed2 = max(ed2, sum);//保证ed2最大
ed1 = min(ed1, sum);//保证ed1最小
if (sum < start)
st1 = max(st1, sum);//这次时间比初始时间要小 更新st1
else if (sum > start)
st2 = min(sum, st2);//这次时间比初始时间要大 更新st2
}
ans = min(min(suma - (start - st1), suma - (st2 - start)), min(start - ed1 + ed2 - ed1, ed2 - start + ed2 - ed1));
/*四种情况中的最小值
第一种 从起点顺时针转到起点的前一个时间(不断增大时间
第二种 从起点逆时针转到起点的后一个时间(不断减小时间
第三种 先逆时针转到最小的 然后在顺时针找到最大的
第四种 先顺时针转到最大的 然后再逆时针找到最小的
*/
ans *= 6;//1s为6°
printf("%d.00\n", ans);
}
return 0;
}







Comments | NOTHING