英文题目:
题目描述
The king is dead, long live the king! After the sudden death of the king Fert XIII the people of the Flatland Kingdom are going to welcome the new king. Unfortunately, there is a problem, Fert has too many sons.
Actually, he has n sons and he loved each new son more than all of his previous sons. Well, probably he just stopped loving his sons because of their bad behavior. Anyway, after the new son was born Fert made the new testament that declared that the newly born son would be the heir.
However, there is a problem. Only the king's son who is at least 18 years old at the moment of the ℎking′s death can become a new king. Now the ministers of the government are trying to find the correct new king, but they seem to fail. Help them!
输入格式
The first line of the input contains three integers: d,m and y -- the day, the month and the year of the king's death, d is from 1 to 31 , m is from 1 to 1,y is from 1 to 9999 . It is guaranteed that there exists day d in month m , all months have the same number of days in Flatland as in our country, except that Flatland calendar doesn't have leap years, so February (month 2) always has 28 days.
The second line contains n(1≤n≤100) -- the number of king's sons. The following n lines contain three integers each di,mi and yi and specify the birth dates of king's sons. All dates are correct and no son is born after or on the day of king's death. The king had no twins, so no two sons were born on the same date.
输出格式
Output one integer -- the number of the son that would become the king, or −1 if none of them is at least18 years old. The sons are numbered from 1 to n in order they are described in the input. The youngest son who is at least 18 years old at the moment of the king's death would become the king. If the son has his 18th birthday exactly on the day of the king's death, he can become a king.
题目翻译
题目描述
国王死了,国王万岁!国王费尔特十三世突然去世后,平原王国的人民将欢迎新国王。不幸的是,有一个问题,弗特的儿子太多了。
事实上,他有n个儿子,他爱每一个新儿子胜过爱他以前的所有儿子。嗯,也许他只是因为儿子们的不良行为而不再爱他们了。无论如何,在新儿子出生后,弗特立了一份新的遗嘱,宣布新出生的儿子将成为继承人。
然而,存在一个问题。只有国王的儿子在ℎ国王的死可以成为新的国王。现在政府的大臣们正在努力寻找合适的新国王,但他们似乎失败了。帮帮他们!
输入格式
输入的第一行包含三个整数:d、m和y——国王去世的日期、月份和年份,d从1到31,m从1到1,y从1到9999。保证m月有d天,除了平地历没有闰年,所以二月(2月)总是有28天之外,平地的所有月份的天数都和我国相同。
第二行包含n(1≤n≤100)——国王的儿子数量。以下n行包含三个整数,每个di,惯性矩和易并规定国王儿子的出生日期。所有日期都是正确的,国王死后或死当天没有儿子出生。国王没有双胞胎,所以没有两个儿子在同一天出生。
输出格式
输出一个整数——将成为国王的儿子的数量,或者−1,如果他们中没有一个年满18岁。子项按输入中描述的顺序从1到n进行编号。国王去世时,最小的儿子年满18岁,将成为国王。如果儿子的18岁生日恰好在国王去世的那天,他就可以成为国王。
题意翻译
在国王去世的那一天,国王会让年龄最小但已满十八周岁的儿子继承国王,求哪一个儿子继承了国王,若没有儿子满足条件,则输出-1
。保证国王不会有双胞胎。
输入输出样例
输入 #1
22 10 2016 7 28 2 1999 22 7 1995 21 10 1998 23 10 1998 3 9 2000 1 4 2013 17 12 2004输出 #1
3
输入 #2
22 10 2016 1 28 2 1999输出 #2
-1
说明/提示
时间限制:2秒,内存限制:256 MB
题目难度
普及-
参考思路
国王有 n 个儿子,他想要把王位传给年龄超过 18 岁的中最小的儿子。题目保证不存在两个儿子在同一天出生。假设所有年份的 2 月均为28 天。
考虑先根据生日将所有儿子排序,然后枚举并计算与国王生日的差即可。排序时可以写比较函数,根据年、月、日三个关键字排序。那么怎么计算两个日期之间的间隔呢?
分两种情况:在同一年或不在同一年。
对于在同一年的,我们枚举两个月之间的完整月,将结果加上该月的天数,再通过简单推导计算出两侧零散月的天数即可。
对于不在同一年的,枚举中间年份加上 365,然后枚举两侧零散年按照上面类似方法计算即可。
参考代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105, mx = 18 * 365;
const int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n;
struct Node {
int d, m, y, id;
Node(int a=0, int b=0, int c=0, int d=0) : d(a), m(b), y(c), id(d) {}
~Node() {}
void read() {scanf("%d%d%d", &this->d, &this->m, &this->y);}
friend bool operator < (const Node &a, const Node &b) {
if(a.y != b.y) return a.y < b.y;
if(a.m != b.m) return a.m < b.m;
return a.d < b.d;
}
}king, son[N];
int calc(const Node &a, const Node &b) {
int res = 0;
if(a.y == b.y) {
if(a.m == b.m) return b.d - a.d + 1;
for(int i=a.m+1;i<b.m;i++) res += month[i];
res += month[a.m] - a.d + 1 + b.d;
return res;
}
for(int i=a.y+1;i<b.y;i++) res += 365;
for(int i=a.m+1;i<=12;i++) res += month[i];
for(int i=1;i<b.m;i++) res += month[i];
res += month[a.m] - a.d + 1 + b.d;
return res;
}
int main() {
king.read();
scanf("%d", &n);
for(int i=1;i<=n;i++) son[i].read(), son[i].id = i;
sort(son+1, son+1+n);
for(int i=n;i;i--) if(calc(son[i], king) > mx) return printf("%d\n", son[i].id), 0;
puts("-1");
return 0;
}