VJ匿名虐(1)
来源:互联网 发布:webbench linux 编辑:程序博客网 时间:2024/05/19 16:35
大年初三,早上睡到12点起来,,QAQ,然后下午准备和家人出去转转,,于是呢,再等家人洗车的过程中,先在VJ上立马查找public正在进行的比赛,找到一个2题的,立马进去。。。不到15分钟,AK结束~,,那个开题的版主不要鄙视我,,怪你的题太水了。。。
A题:
Description
Problem B: Throwing cards away I
Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: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
7191060
Output for sample input
Discarded cards: 1, 3, 5, 7, 4, 2Remaining card: 6Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14Remaining card: 6Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8Remaining card: 4Discarded cards: 1, 3, 5, 2, 6Remaining 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 500000struct 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;}
- VJ匿名虐(1)
- 170411 VJ题解(1)(ZOJ
- 模拟测试(vj)
- VJ pet(DFS)
- vj-苹果(01背包)
- SDUT-VJ-Wormholes(Bellman算法)
- 170411 VJ题解(2)(ZOJ
- 170411 VJ题解(3)(ZOJ
- 170411 VJ题解(4)(ZOJ
- 170411 VJ题解(5)(ZOJ
- VJ组队赛->Chip Factory(5536)
- VJ组队赛->House Building(5538)
- VJ组队赛->Almost Sorted Array(5532)
- VJ组队赛->Dancing Stars on Me(5533)
- VJ 【队列】
- VJ 【STL】
- VJ【背包】
- VJ Pots
- 动态规划学习系列——划分DP(一)
- hdu 5037 Frog
- WPF做的小型音乐播放器-升级版
- WPF做的小型下载工具和一点点吐槽
- WPF制作的小型笔记本-仿有道云笔记
- VJ匿名虐(1)
- 常见的JavaScript代码(七)
- 使用DOM创建和写入xml文件
- 二叉树层次遍历,VUA122
- WPF制作的天气预报小工具
- 10天学会跨平台移动应用开发之AppCan视频教程
- 常见的JavaScript代码(六)
- 亿忆网客户端源码开源!欢迎探讨!
- 常见的JavaScript代码(八)