大年初三,早上睡到12点起来,,QAQ,然后下午准备和家人出去转转,,于是呢,再等家人洗车的过程中,先在VJ上立马查找public正在进行的比赛,找到一个2题的,立马进去。。。不到15分钟,AK结束~,,那个开题的版主不要鄙视我,,怪你的题太水了。。。
A题:
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
Problem B: Throwing cards away I
![](http://uva.onlinejudge.org/external/109/p10935.jpg)
Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck.
Your task is to find the sequence of discarded cards and the last, remaining card.
Each line of input (except the last) contains a number n ≤ 50. The last line contains 0 and this line should not be processed. For each number from the input produce two lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No line will have leading or trailing spaces. See the sample for the expected format.
Sample input
7 19 10 6 0
Output for sample input
Discarded cards: 1, 3, 5, 7, 4, 2 Remaining card: 6 Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14 Remaining card: 6 Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8 Remaining card: 4 Discarded cards: 1, 3, 5, 2, 6 Remaining card: 4
Folklore, adapted by Piotr Rudnicki
解题思路:就是一个裸裸的队列模型,只要判断好最后一个元素的位置,然后将它记录并且输出就OK了。
代码:
# include<cstdio>
# include<iostream>
# include<queue>
using namespace std;
queue<int>q;
int main(void)
{
int n;
while ( cin>>n )
{
if ( n==0 )
break;
for ( int i = 0;i < n;i++ )
{
q.push(i+1);
}
printf("Discarded cards: ");
int sum = 0;
int last = 0;
while ( !q.empty() )
{
sum++;
if ( sum==1 )
printf("%d",q.front());
else
printf(", %d",q.front());
q.pop();
q.push(q.front());
if ( sum==n-1 )
{
last = q.front();
continue;
}
q.pop();
}
cout<<endl;
printf("Remaining card: %d\n",last);
}
return 0;
}
B题:
Problem E
Foreign Exchange
Input: standard input
Output: standard output
Time Limit: 1 second
Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.
The program your organization runs works as follows: All candidates are asked for their original location and the location they would like to go to. The program works out only if every student has a suitable exchange partner. In other words, if a student wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to500000 candidates!
Input
The input file contains multiple cases. Each test case will consist of a line containing n - the number of candidates (1≤n≤500000), followed by nlines representing the exchange information for each candidate. Each of these lines will contain 2 integers, separated by a single space, representing the candidate's original location and the candidate's target location respectively. Locations will be represented by nonnegative integer numbers. You may assume that no candidate will have his or her original location being the same as his or her target location as this would fall into the domestic exchange program. The input is terminated by a case where n = 0; this case should not be processed.
Output
For each test case, print "YES" on a single line if there is a way for the exchange program to work out, otherwise print "NO".
Sample Input Output for Sample Input
10 1 2 2 1 3 4 4 3 100 200 200 100 57 2 2 57 1 2 2 1 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 0 | YES NO |
Problem setter: Gilbert Lee, University of Alberta, Canada
解题思路:
大体就说的是,给你一个学生的org和des,也就是他生在哪里,他想去哪里,然后在n个学生中进行匹配,看看有没有和你刚好意向相仿的学生,什么是意向相仿呢,,就是说,如果你在A要去B,刚好有个骚年在B想去A,那么你俩就可以从完成交换,其实,没什么,只要把这些学生放在一个结构体数组中,然后按org sort一遍,再按照des,saot一遍。在扫一遍,看看按照相同的排序规则得到的元素是不是都相等就OK了。。。
代码:
# include<cstdio>
# include<iostream>
# include<algorithm>
using namespace std;
# define MAX 500000
struct node
{
int org;
int des;
}a[MAX];
int b[MAX];
int c[MAX];
int cmp1( const struct node & x,const struct node & y )
{
return x.org < y.org;
}
int cmp2 ( const struct node & x,const struct node & y )
{
return x.des < y.des;
}
int main(void)
{
int n;
while ( cin>>n )
{
if ( n==0 )
break;
for ( int i = 0;i < n;i++)
{
cin>>a[i].org>>a[i].des;
}
sort( a,a+n,cmp1 );
for ( int i = 0;i < n;i++ )
{
b[i] = a[i].org;
}
sort( a,a+n,cmp2 );
for ( int i = 0;i < n;i++ )
{
c[i] = a[i].des;
}
int flag = 0;
for ( int i = 0;i < n;i++ )
{
if ( b[i]-c[i]!=0 )
{
flag = 1;
break;
}
}
if ( flag )
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}