HDU-6669-Game (analog, greedy)

link:

https://vjudge.net/problem/HDU-6669

Meaning of the questions:

Bears degree of playing a fun game.
Hero of the game a number of standing axis, he can move on the number line, for each movement, he can go to the right or left to select one cell or two cells.
Now he has to in order to complete n tasks, for task i, as long as he is in the interval [ai, bi] on, even if the completion of the task.
Degree of Bear wanted to know, in order to complete all the tasks, at least you need to move many times?
Degree, bears the initial position can be selected arbitrarily.

Ideas:

At first thought it was greedy, press y took a sequence, crazy wa, to find a solution to a problem is found according to the order .....
maintain the current range is located, and then find from the recent run around on the line.

Code:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e3+10;

struct Node
{
    int x, y;
}node[MAXN];
int n;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1;i <= n;i++)
            cin >> node[i].x >> node[i].y;
        int res = 0;
        int l = node[1].x, r = node[1].y;
        for (int i = 2;i <= n;i++)
        {
            if (node[i].x >= l && node[i].y <= r)
                l = node[i].x, r = node[i].y;
            else if (node[i].x >= l && node[i].x <= r && node[i].y > r)
                l = node[i].x;
            else if (node[i].x < l && node[i].y >= l && node[i].y <= r)
                r = node[i].y;
            else if (node[i].x > r)
            {
                res += (node[i].x-r+1)/2;
                l = node[i].x, r += ((node[i].x-r+1)/2)*2;
                r = min(r, node[i].y);
            }
            else if (node[i].y < l)
            {
                res += (l-node[i].y+1)/2;
                r = node[i].y, l -= ((l-node[i].y+1)/2)*2;
                l = max(l, node[i].x);
            }
        }
        cout << res << endl;
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11372407.html