#2 #7 #8 #10 TLE(1.20s)
#6 WA On line 1 column 3, read 9, expected 8.
贪心求解
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
string fill(int s, int k) {
string str = to_string(s);
for (int i = 0; i < k - to_string(s).size(); i++) {
str = "0" + str;
}
return str;
}
int main() {
int n;
cin >> n;
int ques[5001];
for (int i = 0; i < n; i++) {
cin >> ques[i];
}
string start, end;
cin >> start >> end;
int sy = stoi(start.substr(0, 4)), sm = stoi(start.substr(5, 2)), sd = stoi(start.substr(8, 2)), sh = stoi(
start.substr(11, 2)), smin = stoi(start.substr(14, 2));
int lastMin = 0;
while (fill(sy, 4) + "-" + fill(sm, 2) + "-" + fill(sd, 2) + "-" + fill(sh, 2) + ":" + fill(smin, 2) != end) {
smin++;
lastMin++;
if (smin >= 60) {
sh++;
smin = 0;
}
if (sh >= 24) {
sd++;
sh = 0;
}
if ((sm == 1 || sm == 3 || sm == 5 || sm == 7 || sm == 8 || sm == 10 || sm == 12) && sd >= 31) { // 大月
sm++;
sd = 0;
} else if (sm == 2) { // 二月
if (((sy % 4 == 0 && sy % 100 != 0) || sy % 400 == 0) && sd >= 29) { // 闰年
sm++;
sd = 0;
} else if (sd >= 28) { // 平年
sm++;
sd = 0;
}
} else if (sd >= 31) { // 小月
sm++;
sd = 0;
}
if (sm >= 12) {
sy++;
sm = 0;
}
}
sort(ques, ques + n);
int sum = 0;
int num = 0;
for (int i = 0; i < n; i++) {
if (sum + ques[i] <= lastMin) {
sum += ques[i];
num++;
} else {
break;
}
}
cout << num << endl;
return 0;
}