uva 10570——Meeting with Aliens
来源:互联网 发布:mac版ps怎么安装字体 编辑:程序博客网 时间:2024/05/29 13:52
题意:输入1-n的排列,每次可以交换两个整数。用最少的次数把排列变成1-n的环状序列。
思路:枚举+贪心。依次枚举环上所有的点,正序一遍,倒序一遍,然后贪心求的所需的最小步数(贪心策略是1与1号交换,2与2号交换……)。
code:
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <sstream>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef long double ld;const int INF=0x3fffffff;const int inf=-INF;const int N=1000000;const int M=505;const int mod=1000000007;const double pi=acos(-1.0);#define cls(x,c) memset(x,c,sizeof(x))#define cpy(x,a) memcpy(x,a,sizeof(a))#define fr(i,s,n) for (int i=s;i<=n;i++)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define lowbit(x) (x&-x)#define pii pair<int,int>#define mk make_pair#define IN freopen("in.txt","r",stdin);#define OUT freopen("out.txt","w",stdout);int n,vis[M],v[M*2];int sol(int *p){ cls(vis,0); int ct=0,k; fr (i,1,n) { if (vis[i]) continue; ct++;k=i; while (!vis[k]) vis[k]=1,k=p[k]; } return n-ct;}int main(){ while (~scanf("%d",&n)&&n) { fr (i,1,n) scanf("%d",&v[i]); int ans=INF; fr(i,1,2) { fr (j,1,n) v[j+n]=v[j]; fr(j,1,n) ans=min(ans,sol(v+j)); reverse(v+1,v+1+n); } printf("%d\n",ans); }}
0 0
- uva 10570——Meeting with Aliens
- uva 10570—— Meeting with Aliens
- UVA 10570 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- uva 10570 - Meeting with Aliens
- uva 10570 Meeting with Aliens
- UVa 10570 Meeting with Aliens
- UVA - 10570 Meeting with Aliens
- UVa - 10570 - Meeting with Aliens
- uva 10570 meeting with aliens
- UVA 10570 Meeting with Aliens
- UVA 10570 Meeting with Aliens
- Meeting with Aliens UVA
- Meeting with Aliens UVA
- UVA - 10570 Meeting with Aliens 暴力
- UVa 10570:Meeting with Aliens(贪心)
- UVa 10570 Meeting with Aliens(思路)
- 【鸟哥的linux私房菜-学习笔记】Bash shell之管线命令
- 【疯狂的架构】牛公司组织结构图一览:华为,阿里,腾讯,百度,新浪……
- Android团队
- Objective-c的多态
- android studio中api 23的消息推送机制
- uva 10570——Meeting with Aliens
- malloc问题解决
- JAVA基础2-标识符、关键字、数据类型与运算符
- 2015.11.1
- Android开发者网址导航
- Material Design 拾荒记(二) RecyclerView Simple
- Fast Median Filtering Based on FPGA
- MSVC 2013 + Qt 5.4.2 + quazip 0.7.1 的编译及使用
- 控制图片宽度或者高度且使图片不失真