Baidu Star 2019 · Programming Contest - a preliminary round Game HDU 6669 (implementation, greedy)

Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 833 Accepted Submission(s): 200

Problem Description
of the degree Bears are 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.

Input
first line an integer T (1≤T≤10) represents the number of data sets.
For each test, the first row of an integer n (1≤n≤1000) represents the number of tasks.
Next row n, the i-th row two integers ai, bi (1≤ai≤bi≤1000000) indicates that the task corresponding to the interval.

Output
For each test, a line integer answer.

Sample Input
1
2
1 10
20 30

Sample Output
5

Sample Description
Select 10 as a starting point, after the trajectory 10-12-14-16-18-20.

Source
2019 Nian Baidu Star · Programming Contest - a preliminary round

Idea:
As the subject requires a complete n tasks, the first given n intervals, and for the reduction cnt disjoint intervals, then turn into the greedy each interval from the first one,

With a second section and a positional relationship between the first section, determining an initial position,

When now moves from the current i-th interval required to move the distance is an odd number, according to i + 1 (if present) to determine whether a multi-hop location.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
pii a[maxn];
pii b[maxn];
int cnt;
int n;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    int t;
    gbtb;
    cin >> t;
    while (t--) {
        cnt=0;
        cin >> n;
        repd(i, 1, n) {
            cin >> a[i].fi >> a[i].se;
        }
        b[++cnt]=a[1];
        repd(i,2,n)
        {
            if(a[i].fi>b[cnt].se)
            {
                b[++cnt]=a[i];
            }else if(a[i].se<b[cnt].fi)
            {
                b[++cnt]=a[i];
            }else
            {
                b[cnt].fi=max(b[cnt].fi,a[i].fi);
                b[cnt].se=min(b[cnt].se,a[i].se);
            }
        }
        if(cnt==1)
        {
            cout<<0<<endl;// 只有一个区间,直接特判出0
            continue;
        }
        int ans=0;
        int now;// 当前的位置
        if(b[2].fi>b[1].se)
        {
            now=b[1].se;
        }else
        {
            now=b[1].fi;
        }
        repd(i,2,cnt)
        {
            int dis;// 需要移动的距离
            if(b[i].fi>now)
            {
                dis=b[i].fi-now;
                if(dis&1)
                {
                    if(i+1<=cnt&&(b[i].se-b[i].fi)>1&&b[i+1].fi>b[i].fi)
                    {
                        now=b[i].fi+1;
                    }else
                    {
                        now=b[i].fi;
                    }
                }else
                {
                    now=b[i].fi;
                }
                ans+=dis/2;
                if(dis&1)
                {
                    ans++;
                }
            }else
            {
                dis=now-b[i].se;
                if(dis&1)
                {
                    if(i+1<=cnt&&(b[i].se-b[i].fi)>1&&b[i+1].se<b[i].se)
                    {
                        now=b[i].se-1;
                    }else
                    {
                        now=b[i].se;
                    }
                }else
                {
                    now=b[i].se;
                }
                ans+=dis/2;
                if(dis&1)
                {
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11374051.html