Baidu Star 2019 preliminary round a 1002 Game

Portal

Problem Description
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  [ A i , b i ]  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
A first line integer  T ( . 1 T 10 )   represents the number of data sets.
For each test, the first row of an integer  n- ( . 1 n- 1000 )   indicates the number of tasks.
Subsequently  n  row, the  i  row two integers  A i , B i ( . 1 A i B i 1000000 )   represents an interval corresponding to the task.
 


Output
For each test, a line integer answer.
 


Sample Input
1
2
1 10
20 30
 
Sample Output
5
 
 
Sample Description
10 selected as a starting point, after the track was 10-12-14-16-18-20.
 


Source

 

 

Question is intended: to n sections, you need to turn through each segment, optionally the initial position, each movable step or two steps. Q. How many times requires a minimum of movement.

Solution: In order to have time to enter the interval overlap part of the merger, after the completion of the merger only if the last one section so you choose the initial position of any point within this range you can not move. When the combined interval number greater than 1 and a section on the left is not a constant interval on its right we can determine a positional relationship between the first section and the second section in accordance with the initial position of the left end of the first section point or the right point. And because you can skip a step or two steps, then you need to determine is the last step by step better jump or two steps better. If the distance is even or jump to jump to a section of length 1, it is clear there is only one jump method. Otherwise, you can jump in the direction of the past, you then move one space, then we can make a mark, if the next jump and jump the same direction this time that we will jump to the last point and then calculate the number of mobile move .

Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
int a[N],b[N];
int main() {
    int T,n;
    for (scanf("%d",&T);T--;) {
        scanf("%d",&n);
        for (int i = 0; i < n; i++)
            scanf("%d%d",&a[i],&b[i]);
        int l = a[0],r = b[0],i;
        for (i = 1; i < n; i++) {
            int L = max(l,a[i]);
            int R = min(r,b[i]);
            if (L > R) break;
            l = L; r = R;
        }
        if (i == n) {
            printf("0\n");
            continue;
        }
        int fg = 0,ans = 0,x;
        if (b[i] < l) x = l;
        else x = r;
        for (; i < n-1; i++) {
            if (x > b[i]) {
                if (fg == -1) x--;
                ans += (x-b[i]+1)/2;
                if ((x-b[i])%2 == 1 && b[i] - a[i] > 0 ) fg = -1;
                else fg = 0;
                x = b[i];
            }else if (x < a[i]) {
                if (fg == 1) x++;
                ans +=(a[i] - x + 1) /2;
                if ((a[i]-x)%2 == 1 && b[i] - a[i] > 0 ) fg = 1;
                else fg = 0;
                x = a[i];
            }
        }
        if (x > b[i]) {
            if (fg == -1) x--;
            ans += (x-b[i]+1)/2;
        }else if (x < a[i]) {
            if (fg == 1) x++;
            ans +=(a[i] - x + 1) /2;
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/l999q/p/11407056.html