Physical Education CodeForces
来源:互联网 发布:辣妈淘美美 淘宝达人 编辑:程序博客网 时间:2024/06/12 18:58
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order:a1, a2, ..., an, whereai is the height of thei-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order:b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line containsn space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying thei-th place must possess. The third line containsn space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying thei-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e.a and b coincide as multisets.
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimizek but it must not exceed 106. Then print k lines each containing two space-separated integers. Linepi,pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying placespi andpi + 1.
41 2 3 23 2 1 2
42 31 23 42 3
21 1005001 100500
0
题意 n个序列,a[n]与b[n],交换b[n]序列,让b[n]与a[n]相等 输出交换的次数,以及位置
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int M=310;struct st{ int n; int pos;}a[M],b[M];int n;int vis[M];//判断之前是否有b[n]与a[n]对应typedef pair<int,int>P;int main(){ while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) { scanf("%d",&a[i].n); a[i].pos=i; } for(int i=0;i<n;i++) { scanf("%d",&b[i].n); for(int j=0;j<n;j++)//记录b[n]序列应该在a[n]序列的位置 { if(a[j].n==b[i].n&&!vis[a[j].pos]) { b[i].pos=a[j].pos; vis[a[j].pos]=1; break; } } } int t; queue<P>s;//用于保存交换的下标值 for(int i=n-1;i;i--) { for(int j=0;j<i;j++) { if(b[j].pos>b[j+1].pos) { s.push(P(j+1,j+2)); t=b[j].pos; b[j].pos=b[j+1].pos; b[j+1].pos=t; } } } printf("%d\n",s.size()); while(s.size()) { P x=s.front(); printf("%d %d\n",x.first,x.second); s.pop(); } } return 0;}
- Physical Education CodeForces
- Codeforces 394D Physical Education and Buns
- Physical Education
- Codeforces 394D Physical Education and Buns(暴力)
- Codeforces 394D Physical Education and Buns 胡搞
- CF-394D Physical Education and Buns
- URAL 2052 Physical Education(数位dp+二分)
- URAL 2052 . Physical Education (数位DP + 二分)
- URAL 2052 Physical Education(数位dp)
- Codeforces Education round17
- Codeforces Beta Round #90 C. Education Reform
- Codeforces Beta Round #90, problem: (C) Education Reform DP
- codeforces 119C Education Reform (dp+路径打印)
- Codeforces 119C Education Reform【dp+路径输出】
- Education Background
- American Education
- Ideal education
- Education Pays
- Redis设实
- android.content.ActivityNotFoundException:
- activity5工作流(六)
- 基于海量弱特征的AI风控模型正渐渐到来
- MFC源码解读(1)- stdafx.h
- Physical Education CodeForces
- python编程常见错误笔记
- linux的squid代理服务
- href和src的区、link和@import的区别
- Eclipse报错:Subversion Native Library Not Available
- 爬虫实战7-应对反爬虫的策略
- Unity3D
- android mvc
- PHP 正则preg_match 与 preg_match_all 函数以及匹配中文