Notes - greedy problems delayed decision-making

The common feature of this type of problem is after receiving a quota in no hurry to go places with, to retain the quota, when traversing back to the force majeure and then discard the worst of several places. Note that sometimes the same places may receive several times, the last time when it is possible to receive the subject (of course, can make multiple entries teams, but this time to judge whether the team is a valid the team).

https://codeforces.com/contest/1271/problem/D

D - Portals

Meaning of the questions: Are you going to conquer the order of n castles on the line, the conquest of the i-th Castle ai need to have at least two fighters (just need to have, and do not consume), a soldier will receive bi after the completion of the occupation. You can leave a soldier stationed to the castle, get stationed ci score. There are two ways stationed: i point you in, you can manning the point i; or you point at u, u-> v there portal, then you can manning v points. Portal will only lead to the front of the castle. We can not go back, if you lose the occupation failed to finish. Seeking the maximum score, note that you can also use the portal and stationed in n points After the occupation number n Castle.

Solution: even the implied meaning of the questions have to be greedy? Looks a lot like fishing in troubled waters CCPC network master race that question. First, not all forcibly occupied stationed see if it is solvable. Then every step will have a surplus value, the minimum value is that you can take to send the surplus value assigned to the highest overall score of the castle on the line. Then the excess of the value of the audience has fallen, there will be an encounter of 0, if the last hit the back of the castle as well as the excess value of 0 castle, you can send for the highest score to this section of the castle can even place. That is the maximum value of each is to ask castle set and its castle that contains the end of a set of portal contained inside.

Then put the castle each playing a soldier and sent to the portal to stay as far as possible, and then put these soldiers into a small heap root inside waiting for recall, if need more soldiers when we should recall him and remove score mark and occupied the castle . Notice that each castle withdraw up to 5,000 soldiers and then sent 5000 soldiers, only 5,000 castles, and then set a log is more explosive.

However, this algorithm is very problematic, a patch is still very problematic, the problem is that each time the greedy take the castle, but sometimes the good air out of the castle to the back of the portal to get better.

Even stationed in no hurry to take the castle, then the situation will certainly be enough soldiers to remove a portion stationed in places, to remove what does? The removal of the smallest obviously wrong, because it is also possible to remove large behind have the opportunity to re-send the last time there was idle while stationed soldiers. Found a very obvious greedy, sent stationed at the same time the castle by the better after, each castle is actually only the last point to its portal valid. When the portal if there is no idle soldiers no chance. But we can keep this castle first places, only when the manpower is not enough time will have a minimum value and the castle because currently there is no way to hold the castle and then later stationed been eliminated. Reverse traversal can calculate the amount of idle time for each point soldiers will find that after a certain dangerous levels only emptiness will suddenly rise.

int a[5005], b[5005], c[5005];
int prefixb[5005], dif[5005];
int from[5005];
vector<int> to[5005];
priority_queue<pii, vector<pii>, greater<pii> >pq;

void test_case() {
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= n; ++i) {
        scanf("%d%d%d", &a[i], &b[i], &c[i]);
        from[i] = i;
    }
    for(int i = 1, u, v; i <= m; ++i) {
        scanf("%d%d", &u, &v);
        from[v] = max(from[v], u);
    }
    for(int i = 1; i <= n; ++i)
        to[from[i]].push_back(i);
    for(int i = 1; i <= n; ++i) {
        prefixb[i] = b[i] + prefixb[i - 1];
        dif[i] = k + prefixb[i] - a[i + 1];
    }
    for(int i = n - 1; i >= 1; --i)
        dif[i] = min(dif[i], dif[i + 1]);
    if(dif[1] < 0) {
        puts("-1");
        return;
    }
    for(int i = 1; i <= n; ++i) {
        for(auto &v : to[i])
            pq.push({c[v], v});
        while(pq.size() > dif[i])
            pq.pop();
    }
    int sum = 0;
    while(pq.size()) {
        sum += pq.top().first;
        pq.pop();
    }
    printf("%d\n", sum);
}

Summary: this problem is not to get the castle immediately in accordance with the time value greedy, because there may be behind the newly updated large castle it is possible now to remove large castle better. The first observation is sent only when the last chance castle need to send him to go before this is certainly the best to come along with the soldiers. Even got sent to the castle of the quota is not immediately sent soldiers in the past, it is reserved until the last qualifying school for some reason disqualified. The reason here is because the number of disqualified castle at some point stringent than the number of soldiers behind to use the least, on the choice of the worst castle eliminated.

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/12048472.html