B - Seating On Bus
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 660B
Description
Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.
Consider that m (m ≤ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below:
1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, … , n-th row left window seat, n-th row right window seat.
After occupying all the window seats (for m > 2n) the non-window seats are occupied:
1-st row left non-window seat, 1-st row right non-window seat, … , n-th row left non-window seat, n-th row right non-window seat.
All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.
1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, … , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat.
The seating for n = 9 and m = 36.
You are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus.
Input
The only line contains two integers, n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 4n) — the number of pairs of rows and the number of passengers.
Output
Print m distinct integers from 1 to m — the order in which the passengers will get off the bus.
Sample Input
Input
2 7
Output
5 1 6 2 7 3 4
Input
9 36
Output
19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18
这个题不知道怎么简便地做,自己做了很久,也调了很久,最后出来了额,但是学长他们还有队友一次就A了,他们呢说用栈的方法,目前还不知道怎么用,所以先放在这了。
代码:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,m;
int a[100+5][2],b[100+5][2];
while(cin>>n>>m)
{
if(n==1)
{
if(m==1)
cout<<"1"<<endl;
if(m==2)cout<<"1"<<" "<<"2"<<endl;
if(m==3)cout<<"3"<<" "<<"1"<<" "<<"2"<<endl;;
if(m==4)cout<<"3"<<" "<<"1"<<" "<<"4"<<" "<<"2"<<endl;
}
else {
for(int i=0;i<=n;i++)
{
memset(a[i],0,sizeof(a[i]));
memset(b[i],0,sizeof(b[i]));
}
if(m<=2*n)
{
for(int i=1;i<m;i++)
cout<<i<<" ";
cout<<m<<endl;
}
else
{
int k=1;
int i=1;
while(k<=2*n)
{
a[i][0]=k++;
if(k<=2*n)
a[i++][1]=k++;
}
i=1;
while(k<=m)
{
b[i][0]=k++;
if(k<=m)
b[i++][1]=k++;
}
int x=(m-2*n)/2;
int y=(m-2*n)%2;
for(int i=1;i<=n;i++)
{
if(i==1)
{
if(y==0||i<=x)
cout<<b[i][0]<<" "<<a[i][0]<<" "<<b[i][1]<<" "<<a[i][1];
else cout<<b[i][0]<<" "<<a[i][0]<<" "<<a[i][1];
}
else
if(i<=x)
cout<<" "<<b[i][0]<<" "<<a[i][0]<<" "<<b[i][1]<<" "<<a[i][1];
else
{
if(y==0||i>x+1)
cout<<" "<<a[i][0]<<" "<<a[i][1];
else
cout<<" "<<b[i][0]<<" "<<a[i][0]<<" "<<a[i][1];
}
}
cout<<endl;
}
}
}
return 0;
}