题目链接:P1178 到天宫做客 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目描述
有一天,我做了个梦,梦见我很荣幸的接到了猪八戒的邀请,到天宫陪他吃酒。我犹豫了。天上一日,人间一年啊!当然,我是个闲人,一年之中也没有多少时日是必须在人间的,因此,我希望选一个最长的空闲时间段,使我在天上待的时间尽量长。记住,今年是 4000 年。天上一天也是 24 小时,每小时 60 分,每分 60 秒。
输入格式
第一行是一个非负整数 N,表示4000年中必须呆在人间的天数。
以下共 N 行,每行两个用空格隔开的正整数,即日期(月,日),输入文件保证无错误,日期无重复。
输出格式
一个非负整数,即在天上的时间(四舍五入精确到 1 秒)。
样例 #1
样例输入 #1
2
3 8
12 2
样例输出 #1
63266
AC code:(模拟 + 排序)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int main()
{
int months[13] = {0 , 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31};
int n;
cin>>n;
vector<pair<int,int>> p(n);
for(int i = 0 ; i < n ; i ++)
cin>>p[i].first>>p[i].second;
sort(p.begin(),p.end());
// if(n == 0 || n > 0 && (p[n - 1].first != 12 && p[n - 1].second != 31))
p.push_back({12,32});
vector<int> days;
for(int i = 0 ; i <= n ; i ++)
{
int m = p[i].first;
int d = p[i].second;
int day = 0;
for(int j = m - 1 ; j >= 1 ; j --)
day += months[j];
day += d;
days.push_back(day);
}
int maxday = days[0] - 1;
for(int i = 1 ; i < days.size() ; i ++)
{
maxday = max(maxday , (days[i] - days[i - 1] - 1));
}
// 268
printf("%.0lf\n" , (maxday * 1.0 / 366) * 24 * 60 * 60);
return 0;
}
892

被折叠的 条评论
为什么被折叠?



