Luo Gu P1167 brush title
Topic links: Luo Gu P1167 brush title
Algorithms 贪心
Tags: ,模拟
topic
Title Description
noip
Near, 小A
only to find that he had not written the subject. Fortunately, now there is some time away from the competition, 小A
decided to brush day and night from now on topic. That 小A
sleepless nights, 24 hours a day to brush the question.
Today's date (time) is min minutes and hh yyyy mm-dd-day, time of examination is yyyy 'in mm' Month dd min 'minutes and' day hh '. All the time between these 小A
are used to brush the question, then the maximum number of questions before the exam he can brush it? Note Well, consider a leap year.
Time constraints 小A
just quantity regardless of quality. Of course, some topics easier, some harder topic. According to 小A
experience, he can see at a glance the time to write a certain topic needed to record minutes.
Now given 洛谷Online Judge
a list of topics, please pick up the topic 小A
can be written before the race.
We assume that from ancient times to the future, the calendar is represented as now.
Input Format
A first line integer N, the 洛谷Online Judg
number of topics e, N≤5000.
Next N rows, each row represents an integer time of the issue requires a brush, in minutes and (≤10000). (This question itself is not what is important, is not it? 小A
Has written several topics to 0).
The next two lines followed by the current time and race time. Format given time is: yyyy-mm-dd-hh: min, for example: 2007-06-23-02: 00, 24-hour, day, from 00: 00 to 23: 59 years from 0000 to 9999.
Output Format
Row, an integer noip
number of the subject before the brush up.
Sample input and output
Input # 1
2
1
1
2007-06-23-11:59
2007-06-23-12:00
Output # 1
1
answer:
Most begin to see the solution to a problem is obvious greedy solution, to ensure that each time within a limited time you can take the minimum (use sort()
), but the difficulty lies in this question of how simulation calculated the total number of minutes. There are some things that require special attention during the simulation:
- leap year
- The number of days each month
- How to read? ? ? (
scanf("%d-%d-%d-%d:%d",……)
)
But then you need to brush up for judgment :( leap year every four years a leap, leap hundred years, four hundred years and then leap)
bool run(int x)
{
if (x % 100 == 0)
{
if (x % 400 == 0)
return 1;
}
else
if (x % 4 == 0)
return 1;
return 0;
}
Then we simulated time, the calculation time processing "Year", "Day", "Hour", "Minute" are relatively simple, according to the band pass to the next, but in the "month" judgment is more complicated, simulation, considering the number of days per month, and in view of this year (12 months) if the case involves a leap year, the specific implementation, see AC codes .
AC Code
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct _TIME{
int y, m, d, h, minn;
}_time[3];
int mon[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool run(int x)
{
if (x % 100 == 0)
{
if (x % 400 == 0)
return 1;
}
else
if (x % 4 == 0)
return 1;
return 0;
}
int getminn(_TIME a, _TIME b)
{
int ansy, ansm, ansd, ansh, ansminn;
ansy = b.y - a.y;
ansm = b.m - a.m;
ansd = b.d - a.d;
ansh = b.h - a.h;
ansminn = b.minn - a.minn;
if (ansy > 0)
ansm += ansy * 12;
if (ansm > 0)
{
int i = a.m, j = a.y;
for (int k = 1; k <= ansm; k ++ )
{
if (i > 12)
{
i -= 12;
j ++ ;
}
if (i == 1 && run(j))
{
ansd += mon[i - 1] + 1;
i ++ ;
continue ;
}
ansd += mon[i - 1];
i ++ ;
}
}
if (ansd > 0)
ansh += ansd * 24;
if (ansh > 0)
ansminn += ansh * 60;
return ansminn;
}
int num[5500];
int n;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%d", &num[i]);
sort(num + 1, num + 1 + n);
scanf("%d-%d-%d-%d:%d", &_time[1].y, &_time[1].m, &_time[1].d, &_time[1].h, &_time[1].minn);
scanf("%d-%d-%d-%d:%d", &_time[2].y, &_time[2].m, &_time[2].d, &_time[2].h, &_time[2].minn);
int allminn = getminn(_time[1], _time[2]);
int cnt = 0;
for (int i = 1; i <= n; i ++ )
{
if (num[i] <= allminn)
{
cnt ++ ;
allminn -= num[i];
}
else
break ;
}
printf("%d\n", cnt);
return 0;
}