NOIP2014 Flying Birds

Source: Internet
Author: User

3. Flying Birds

(Bird.cpp/c/pas)

"Problem Description described "

Flappy Bird is a rage casual mobile phone game. Players need to constantly control the frequency of clicking on the phone screen to adjust the bird's flight altitude, so that the bird smoothly through the screen to the right of the pipe gap. If a bird accidentally hits a water pipe or falls on the ground, it will fail.

To simplify the problem, we have simplified and adapted the rules of the game:

    1. The game interface is a two-dimensional plane with a length of N and a height of M, with a K-pipe (ignoring the width of the pipe).
    2. The bird always moves within the game's interface. The bird starts at the leftmost integer height of the game interface and reaches the far right of the game interface, completing the game.
    3. Birds each unit time along the horizontal axis to the right of the distance is 1, vertical movement of the distance is controlled by the player. If you click on the screen, the bird will rise a certain height x, each unit time can be clicked multiple times, effect overlay; If you do not tap the screen, the bird will drop a certain height y. When the birds are in different positions in the horizontal axis, the ascending height x and the descending height y may differ from each other.
    4. The bird height is equal to 0 or the bird hits the pipe and the game fails. When the bird height is m, it can no longer rise.

Now, tell me if you can finish the game. If possible, output a minimum number of hits on the screen; otherwise, the output bird

The maximum number of pipe gaps that can be passed.

Input

The input file name is bird.in.

The 1th line has 3 integer n,m,k, which indicates the length of the game interface, the height and the number of water pipes, separated by a space between each two integers;

The next n rows, each row of 2 integers separated by a space of X and Y, in order to indicate at the horizontal position 0~n-1 on the player click on the screen, the bird in the next position of the height of the X, and in this position when the player does not click the screen, the bird in the next position down the height of Y.

Next k lines, 3 integers per line p,l,h, separated by a space between each of the two integers. Each line represents a pipe, where p represents the horizontal axis of the pipe, and L indicates that the bottom-edge height of the pipe gap is l,h, which indicates the height of the edge on the pipe gap (the input data guarantees that p varies, but is not guaranteed in order of size).

Output

The output file name is Bird.out.

A total of two lines.

The first line, which contains an integer, outputs 1 if the game can be completed successfully, otherwise outputs 0.

The second line, which contains an integer, if the first behavior 1, the output successfully completed the game requires a minimum number of clicks on the screen, otherwise, the output bird can pass the maximum number of pipe gaps.

"Input and output Example 1"

Bird. in

Bird. out

10 10 6

3 9

9 9

1 2

1 3

1 2

1 1

2 1

2 1

1 6

2 2

1 2 7

5 1 5

6 3 5

7 5 8

8 7 9

9 1 3

1

6

"Input and output Example 2"

Bird. in

Bird. out

10 10 4

1 2

3 1

2 2

1 8

1 8

3 2

2 1

2 1

2 2

1 2

1 0 2

6 7 9

9 1 4

3 8 10

0

3

"Input and Output sample description"

As shown, the blue line represents the bird's flight trajectory, and the red line represents the pipeline.

"Data Range"

For 30% data: 5≤n≤10,5≤m≤10,k=0, ensure that there is a set of optimal solutions to make the same unit time up to click the screen 3 times;

For 50% data: 5≤n≤20,5≤m≤10, ensure that there is a set of optimal solutions to make the same unit time up to click the screen 3 times;

For 70% data: 5≤n≤1000,5≤m≤100;

For 100% of data: 5≤n≤10000,5≤m≤1000,0≤k<n,0<x<m,0<y<m,0<p<n,0≤l

Ideas

The idea of the topic is simple, but it always feels uncomfortable to write.

State transition equation:

D[i][j]=min{d[i-1][j+y[i-1], d[i-1][j-k*x[i-1]+k}

Here the K easy to associate with the full backpack of the infinite items, similar, do not have to enumerate K, changed to d[i][j-x[i]]+1, it is actually the cumulative meaning. It is also important to note that the Y sub-problem should be placed after x, otherwise it will result in a decision overlay.

J==m Special case: Notice that the question says "The bird height is m, can not rise again." "Special handling: Also with cumulative method, D[i][j]=min (d[i][j],d[i][pos]+1) pos from [J-x[i-1],m] became a segment by point.

Fly failure: With a flag, as long as d[i][] All INF then the problem can not be solved, the corresponding output ends.

Code

1#include <iostream>2 #definefor (A,B,C) for (int a= (b);a< (c); a++)3 using namespacestd;4 5 Const intinf=1<< -;6 Const intMAXN =10000+5, MAXM = ++5; 7 intn,m,k;8 intD[MAXN][MAXM];9 intX[MAXN],Y[MAXN],UP[MAXN],DOWN[MAXN];Ten  One intMain () { AIos::sync_with_stdio (false); -      -Cin>>n>>m>>K; thefor (I,0, N) { -Cin>>x[i]>>Y[i]; -up[i]=m+1; down[i]=0;//down=0 up=m+1 -} up[n]=m+1; down[n]=0; +      -for (I,0, K) { +         intP Cin>>p; ACin>>down[p]>>Up[p]; at     } -for (I,1, n+1) for (J,0, m+1) d[i][j]=INF; -d[0][0]=inf;//d declaration is automatically cleared in global variables 0 -      -for (I,1, n+1)//I start from 1 to n end -       { in           BOOLflag=true;  -for (j,down[i]+1, Up[i]) {//write here Miss = = to           if(j-x[i-1]>down[i-1]) { +D[i][j]=min (d[i][j],d[i][j-x[i-1]]+1); -D[i][j]=min (d[i][j],d[i-1][j-x[i-1]]+1); the            } *            if(j==m) for (pos,j-x[i-1],j+1) {//Special cases Special judgment//be careful not to write in the previous if $D[i][j]=min (d[i][j],d[i][pos]+1);Panax NotoginsengD[i][j]=min (d[i][j],d[i-1][pos]+1); -            }  the            if(D[i][j]<inf) flag=false; +         } A         //The decision D[i][j-x[i] "may be transferred to Y in order to find out if the X is not the only way to ask for y). thefor (j,down[i]+1, Up[i])if(j+y[i-1]<up[i-1]) {  +D[i][j]=min (d[i][j],d[i-1][j+y[i-1]]); -          if(D[i][j]<inf) flag=false; $         }  $  -         if(flag) {//If this I cannot exit by that output -             intsum=0; theFor (K,0, i) -                 if(up[k]!=m+1|| down[k]!=0) sum++;//if the pipeline//|| Rather than &&Wuyicout<<0<<"\ n"<<sum; the          return 0; -        } Wu       } -        About     intMini=INF; $For (J,1, m+1) Mini=min (Mini,d[n][j]);//Right-most n -cout<<1<<"\ n"<<Mini; -     return 0; -      A}

NOIP2014 Flying Birds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.