求救!请问为什么会 TLE 5个点啊?

P1167 刷题

yubing_lml @ 2019-09-01 15:10:50

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<stdio.h>
using namespace std;

int n, ans = 0;
int a[5007];
int month[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int y1, m1, d1, h1, min1, y2, m2, d2, h2, min2;
char ch;

bool isLeap(int year)
{
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        return true;
    return false;
}

int daysinyear(int y, int m, int d)  //该天是本年的第几天
{
    for (int i = 1; i < m; i++)
        d += month[i];
    if (isLeap(y) && m > 2)
        d++;
    return d;
}

int days(int y1, int m1, int d1, int y2, int m2, int d2)
{
    if (y1 == y2 && m1 == m2)
        return d2 - d1;
    else if (y1 == y2)
    {
        int ds1 = daysinyear(y1, m1, d1);
        int ds2 = daysinyear(y2, m2, d2);
        return (ds2 - ds1);
    }
    else
    {
        int ds1 = 365 - daysinyear(y1, m1, d1);
        if (isLeap(y1))
            ds1++;
        int ds2 = daysinyear(y2, m2, d2);
        int ds3=0;
        for (int i = y1 + 1; i < y2; i++)
        {
            ds3 += 365;
            if (isLeap(i))
                ds3++;
        }
        return (ds1 + ds2 + ds3);
    }
}

int main()
{
    cin.sync_with_stdio(false);
    cin >> n;
    unsigned long long int need = 0;
    for (int i = 1; i <= n; i++)
        cin >> a[i], need += a[i];
    sort(a + 1, a + 1 + n);
    cin >> y1 >> ch >> m1 >> ch >> d1 >> ch >> h1 >> ch >> min1;
    getchar();
    cin >> y2 >> ch >> m2 >> ch >> d2 >> ch >> h2 >> ch >> min2;
    getchar();
    int dayleft = days(y1, m1, d1, y2, m2, d2);
    unsigned long long int time = 0;
    time += 24 * 60 * dayleft;
    time += (min2 - min1) + (h2 - h1) * 60;
    if (need <= time)
    {
        cout << n;
        return 0;
    }
    while (time > 0)
        if (time >= a[ans + 1])
        {
            time -= a[ans + 1];
            ans++;
        }
    cout << ans;
    return 0;
}

|