Present Problem(UVALive 7000)

Description

A group of persons (indexed from 0 to n − 1) play a game to determine which of the n presents (also
indexed from 0 to n − 1) each of them will get. They play the game on a system of n vertical “lines”,
numbered from 0 to n − 1, from left to right. Person i initially is at the top of line i, and present i
is at the bottom of line i. All lines have length l. There are also horizontal “links” that connect two
adjacent lines i and i + 1 at the same length k, where k is an integer distance from the top end of the
line, and 0 < k < l. We use link (i, k) to denote this link. In addition, two links adjacent to a line
cannot be at the same length of the line. That is, we cannot have both links (i, k) and (i + 1, k).
Person i determines which present he will receive as follows. He will start from the top of line i and
go downward. Whenever he encounters an intersection of a line and a link, he has to make a turn, and
he can go either left or right along a link, or downward along a line. Finally he will reach the bottom
of a line j, and he will receive present j.


Figure 1: An example of 6 lines and 10 links.
We use Figure 1 to illiterate the rules. Person 1 starts from the top of line 1 and goes downward.
He first reaches the link at (1, 1) and makes a turn to line 2, and keeps going downward. Then he
reaches the link at (2, 3) and makes another turn to line 3. He keeps on going, as indicated by the path
in Figure 1, and finally reaches present 4.

题解:

要有可交换的思想,比如图中1可到达2,2也可以达到1。还有注意输出的顺序,有些人得出的是3 2 5 0 1 4,明显是算的下方的,要从后面往前推。

代码如下:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 2017
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;


int n,m,l;
int b[100007];
int s[100007];
struct Node
{
    int x;
    int y;
}a[100007];
int cmp(Node a,Node b)
{
    if(a.y!=b.y)
        return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>l;
        for(int i=0;i<m;i++)
            cin>>a[i].x>>a[i].y;
        sort(a,a+m,cmp);
        for(int i=0;i<n;i++)
            b[i]=i;
        for(int i=0;i<m;i++)
        {
            int p=a[i].x,q=a[i].x+1;
            if(i<m-1&&a[i].x==a[i+1].x||q>=n)
                continue;
            swap(b[p],b[q]);
        }
        for(int i=0;i<n;i++)
        {
            s[b[i]]=i;
        }
        for(int i=0;i<n;i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值