9503 Lazy Choice Seat

Source: Internet
Author: User

9503 Lazy Choice Seat

Time limit: 500MS memory limit: 65535K
Number of submissions: 0 Number of Passes: 0

Question types: programming language: g++; Gcc

    After being mixed up with ACM for a year, I finally got a chance to question the freshman race. Then I got a question in a flash.    (OK, I admit, I just translated the original question only ╮(╯▽╰)╭)
Sent over to Lyd, and then immediately despised: "So simple a question?" " Hey, no way ah, the past year I was water shearing problem ~ ~ ~
In fact, I personally think this question is quite interesting ... is water more healthy? Let's have a water-cut problem together!
I believe we all have this experience: the space between each row of the classroom is very small, in class, the early students have to sideways or stand up, let other students sit inside.
╮(╯▽╰)╭ I am a very lazy person, if I was the first to arrive, I hope to choose a seat, the number of side-by-side can be as small as possible ... There is now a row of n empty seats, numbered 1 ~ N, respectively, from left to right. Then the other students will select a seat and then walk in, and go in the direction (left or right) subject to the following provisions: 1. Choose the direction of the least number of people to enter ①② I ③④⑤⑥ for example, the second to the classmate designated 3rd seats, he will choose to enter from the right, I do not have to give way. 2. When the number of people passing on both ends is the same, select the side that is close to your seat. ①② i ③④⑤ someone ⑥ such as a third to the students choose to sit in seat 3rd, he will enter from the left, and then I will give way. 3. The same time, randomly selected to enter the direction. the question now is, when I choose to sit in the K-seat, I wonder how many times I have to make a side-by-side situation in the worst case?

Input format
    There are several sets of data that end with the EOF decision.    the first row of each group of data has two shaping N (1~ 30) and Q (1~30). Indicates the number of seats and number of queries respectively. The next line has an integral type of Q. K1,K2......KQ, respectively, that I choose to sit in the K-seat. Data is separated by a space.

Output format
    One row per group of data, for each query, output a result, separated by a space.

Input sample
4 41 2 3 45 51 2 3 4 5

Output sample
1 0 0 12 1 0 1 2

    four seats, I choose the seat ①, if the second person ③or④, when the third person ②, I will give way, this is the worst case.     The end of EOF is the data read-in ending with a file terminator. That is, the input and output is similar to the following (of course, the variable name does not have to be the same, VC press CTRL + Z and    Enter to end the program): While    (scanf ("%d%d", &n,&q)!=eof)    {for        (i=1 ; i<=q;i++)        scanf ("%d", &act[i]);        ...        ... printf ("%d\n", answer);    }





Title The Lord says ... Water problem together, although the label is playing math, but it seems that even the law does not need how to find, the solution to the problem can be pit to the main may be some circumstances did not consider: the title is in the worst case, it must be a comprehensive point, so that the number of seats in the title as bad as possible ... ╮(╯▽╰)╭

My solution is to take into account the odd and even number of cases. Let's look at a set of examples in even-numbered cases:

Input:10 10

1 2 3 4 5 6 7 8 9 10

Output:7 6 5 4 3 3 4 5 6 7

Starting from K==1, it is obvious that when you sit on the far left, if you fill in the two seats (9 and 10) on the far right, then the person seated according to the title will choose the one with the fewest number of channels to enter so the fourth person will enter on the left, so the worst case scenario is that 9 and 10 have been seated (   Then the number of seats or 0, and then 7 people from the 8th position all the way to sit (2←8) In this case, sit k==1 position to make 7 times seat for the worst case. Then the k==2 to N/2 method is the same, and the result is symmetrical with n/2+1 to N.

Let's look at a set of examples of odd cases:

Input:11 11

1 2 3 4 5 6 7 8 9 10 11

Output:8 7 6 5 4 3 4 5 6 7 8

The worst-case scenario for an odd-numbered discussion is the same as above, but the corresponding laws of the input and output are small, so there are several sets of data on the paper before the machine code, and the test is true.

/* Code */

#include <stdio.h> #include <math.h>void even (int n,int q); void odd (int n,int q); int main () {    int n,q,i;< C1/>while (scanf ("%d%d", &n,&q)!=eof)    {        int a[q];        for (i=0;i<q;i++)            scanf ("%d", &a[i]);        if (n%2==0) for            (i=0;i<q;i++)                even (N,a[i]);        else for            (i=0;i<q;i++)                odd (N,a[i]);        Putchar (' \ n ');    }    return 0;} void even (int n,int q) {    int middle=n/2;    if (n==2)        {            printf ("0");            return;        }    if (q<=middle)        printf ("%d", n-q-2);    else        printf ("%d", q-3);} void odd (int n,int q) {    int middle=n/2+1;    if (n==1)    {        printf ("0");        return;    }    else if (n==3)    {        printf ("%.lf", Fabs (Middle-q));        return;    }    printf ("%.lf", Fabs (MIDDLE-Q) +middle-3);}


9503 Lazy Choice Seat

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.