POJ 3270 Cow Sorting

题意:

一个序列变为升序,操作为交换两个元素,代价为两元素之和,求最小代价

题解:

看了黑书...

首先循环因子分解

一个循环完成的最小代价要么是循环中最小元素依次与其他交换,要么引入全局最小值来交换

$sum+min(mn*(len-2),mn+Min*(len+1))$

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e4+,M=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,a[N],c[M],Min=M,m;
int f[N],ans;
bool vis[N];
int main(){
//freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++)
a[i]=read(),c[a[i]]++,Min=min(Min,a[i]),m=max(m,a[i]);
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=;i<=n;i++) f[i]=c[a[i]];
for(int i=;i<=n;i++) if(!vis[i]){
vis[i]=;
int u=f[i],sum=a[i],mn=a[i],len=;
while(u!=i) vis[u]=,sum+=a[u],mn=min(mn,a[u]),len++,u=f[u];
ans+=sum+min(mn*(len-),mn+Min*(len+));
}
printf("%d",ans);
}

POJ2369 Permutations

题意:

求一个置换的几次幂得到自身

题解:

每个循环长度的最小公倍数

注意是几次幂不是转换了几次!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e3+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,a[N],ans=;
inline int gcd(int a,int b){return b==?a:gcd(b,a%b);}
inline int lcm(int a,int b){return a/gcd(a,b)*b;}
bool vis[N];
int main(){
//freopen("in","r",stdin);
n=read();
for(int i=;i<=n;i++) a[i]=read();
for(int i=;i<=n;i++) if(!vis[i]){
vis[i]=;
int u=a[i],len=;
while(u!=i) vis[i]=,len++,u=a[u];
ans=lcm(ans,len);
}
printf("%d",ans);
}

POJ1721CARDS

题意:

交换规则为$i \rightarrow a[a[i]]$,进行了$s$次,给出最后的排列求一开始

题解:

一直尝试构造置换的逆一直失败,也许是因为每次合成的置换都不同吧

网上的做法是暴力找整个置换的循环然后把剩下的操作补出来...感觉会被卡...

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,s,a[N],t[N],b[N];
int main(){
freopen("in","r",stdin);
n=read();s=read();
for(int i=;i<=n;i++) b[i]=a[i]=read();
int cnt=;
while(true){
int flag=;
cnt++;
for(int i=;i<=n;i++) t[i]=a[a[i]];
for(int i=;i<=n;i++){
a[i]=t[i];
if(a[i]!=b[i]) flag=;
}
if(flag) break;
}
cnt=cnt-s%cnt;
while(cnt--){
for(int i=;i<=n;i++) t[i]=a[a[i]];
for(int i=;i<=n;i++) a[i]=t[i];
}
for(int i=;i<=n;i++) printf("%d\n",a[i]);
}

POJ置换群入门[3/3]的更多相关文章

  1. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  2. ACM题集以及各种总结大全!

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  3. ACM题集以及各种总结大全(转)

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  4. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  5. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  6. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  7. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  8. Oil Deposits(poj 1526 DFS入门题)

    http://poj.org/problem?id=1562                                                                       ...

  9. POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道

    rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

随机推荐

  1. c++2(循环和递归)

    其实编程的朋友知道,不管学什么语言,循环和递归是两个必须学习的内容.当然,如果循环还好理解一点,那么递归却没有那么简单.我们曾经对递归讳莫如深,但是我想告诉大家的是,递归其实没有那么可怕.所谓的递归就 ...

  2. UEP-自定义持久化类

    HY-UEP提供了两种持久化方式:基于JPA的持久化机制和自定义持久化机制,JPA的持久化机制 比较符合现在的软件开发模式,支持各种主流数据库;自定义持久化时我们平台产品的特有持久化 机制的升级,在综 ...

  3. Dora.Interception, 一个为.NET Core度身打造的AOP框架[3]:Interceptor的注册

    在<不一样的Interceptor>中我们着重介绍了Dora.Interception中最为核心的对象Interceptor,以及定义Interceptor类型的一些约定.由于Interc ...

  4. 改进ban冒泡排序

    设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置.由于pos位置之后的记录均已交换到位,故在进行下一趟排序时只要扫描到pos位置即可. //改进后算法如下: function bubb ...

  5. Android按下home键后重新打开app进入主activity的问题

    问题阐述: 当我们写一款App的时候,势必会有这种情况:用户已经进行了多级的操作,现返回栈中已存在多个activity,那么这个时候我们想回到最初的activity难道要一层层的返回吗,对用户来说 无 ...

  6. WPF 简单的循环GIF播放

    //MVVM要事件绑定,记得项目引用类库“Sysrem.Windows.interactivity”,然后XAML引用 xmlns:i="http://schemas.microsoft.c ...

  7. 使用 video.js 开发 HTML5 视频页面

    时间 2015-05-13 17:11:58 The GIS Guy 原文  http://thegisguy.tk/html5-video-using-video-js/ 主题 Video.js H ...

  8. js立体旋转展示效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 为什么要进行URL编码

    我们都知道Http协议中参数的传输是"key=value"这种简直对形式的,如果要传多个参数就需要用“&”符号对键值对进行分割.如"?name1=value1&a ...

  10. ArrayList 源码详细分析

    1.概述 ArrayList 是一种变长的集合类,基于定长数组实现.ArrayList 允许空值和重复元素,当往 ArrayList 中添加的元素数量大于其底层数组容量时,其会通过扩容机制重新生成一个 ...