제출 #47037

#제출 시각아이디문제언어결과실행 시간메모리
47037SpaimaCarpatilorArranging Tickets (JOI17_arranging_tickets)C++17
100 / 100
3442 ms15156 KiB
#include<bits/stdc++.h>

using namespace std;

int N, M, l[100009], r[100009], c[100009];
long long maxA, sumC, b[200009], a[200009], aib[200009];
vector < pair < int, int > > v[200009];

void U (int pos, int val)
{
    while (pos <= N)
        aib[pos] += val,
        pos += pos - (pos & (pos - 1));
}

long long Q (int pos)
{
    long long sum = 0;
    while (pos)
        sum += aib[pos],
        pos &= pos - 1;
    return sum;
}

bool getVerdict (long long S, long long cntFlipped)
{
    if (cntFlipped > sumC) return 0;
    for (int i=1; i<=N; i++)
    {
        long long newA = a[i] + cntFlipped;
        if (newA < S) b[i] = 0;
        else b[i] = (newA + 1 - S) / 2;
    }
    for (int i=1; i<=N; i++)
        aib[i] = 0;
    long long used = 0;
    priority_queue < pair < int, int > > PQ;
    for (int i=1; i<=N; i++)
    {
        for (auto r : v[i])
            PQ.push (r);
        long long needed = max (0LL, b[i] - Q (N - i + 1));
        while (!PQ.empty ())
        {
            auto curr = PQ.top ();
            PQ.pop ();
            long long f = min (needed, (long long) curr.second);
            used += f, needed -= f;
            U (N - curr.first + 1, +f);
            if (f != curr.second)
                PQ.push ({curr.first, curr.second - f});
            if (needed == 0)
                break;
        }
        if (needed > 0) return 0;
    }
    return (used <= cntFlipped);
}

bool ok (long long S)
{
    if (S >= maxA) return 1;
    if (getVerdict (S, maxA - S)) return 1;
    if (getVerdict (S, maxA - S + 1)) return 1;
    return 0;
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d %d", &N, &M);
///N links: 1-2, 2-3, .., N-1
for (int i=1; i<=M; i++)
{
    scanf ("%d %d %d", &l[i], &r[i], &c[i]), sumC += c[i];
    if (l[i] > r[i]) swap (l[i], r[i]);
    a[l[i]] += c[i], a[r[i]] -= c[i];
    v[l[i]].push_back ({r[i] - 1, c[i]});
}
long long p = 1, u = 0, mij, ras;
for (int i=1; i<=N; i++)
    a[i] += a[i - 1], u = max (u, a[i]);
maxA = u;
while (p <= u)
{
    mij = (p + u) >> 1;
    if (ok (mij)) ras = mij, u = mij - 1;
    else p = mij + 1;
}
printf ("%lld\n", ras);
return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

arranging_tickets.cpp: In function 'int main()':
arranging_tickets.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d", &N, &M);
 ~~~~~~^~~~~~~~~~~~~~~~~
arranging_tickets.cpp:77:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d %d", &l[i], &r[i], &c[i]), sumC += c[i];
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...