poj 3270 Cow Sorting(置换)
来源:互联网 发布:淘宝上情趣内衣买家秀 编辑:程序博客网 时间:2024/05/20 08:01
代码:
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define MAX 10005#define MAXN 1000005#define maxnode 205#define sigma_size 2#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 LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;//const double pi = acos(-1.0);const double inf = 1e18;const double eps = 1e-9;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/vector<int> v[MAX];int a[MAX];int b[MAX];int c[MAX];int vis[MAX*10];int main(){ //freopen("in.txt","r",stdin); int n; cin>>n; mem(vis,0); int cnt=0; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; c[i]=a[i]; v[0].push_back(a[i]); } sort(b+1,b+n+1); sort(v[0].begin(),v[0].end()); for(int i=1;i<=n;i++){ int pos=lower_bound(b+1,b+n+1,a[i])-b; c[i]=pos; } for(int i=1;i<=n;i++){ if(!vis[c[i]]){ ++cnt; v[cnt].push_back(b[c[i]]); vis[c[i]]=1; int k=c[c[i]]; while(k!=c[i]){ vis[k]=1; v[cnt].push_back(b[k]); k=c[k]; } sort(v[cnt].begin(),v[cnt].end()); } } LL ans=0; for(int i=1;i<=cnt;i++){ LL mini=1e18; LL sum=0; if(v[i].size()==1) continue; for(int j=0;j<v[i].size();j++){ sum+=v[i][j]; } sum+=v[i][0]*(v[i].size()-2); mini=min(sum,mini); if(v[0][0]<v[i][0]){ sum=0; for(int j=1;j<v[i].size();j++) sum+=v[i][j]; sum+=(v[i].size()+1)*v[0][0]+2*v[i][0]; } mini=min(sum,mini); ans+=mini; } cout<<ans<<endl; return 0;}
0 0
- POJ 3270 Cow Sorting(置换环)
- poj 3270 Cow Sorting(置换)
- poj 3270 Cow Sorting(置换)
- POJ 3270 Cow Sorting (置换群)
- POJ 3270 Cow Sorting(置换)
- POJ 3270 Cow Sorting(置换)
- POJ 3270 Cow Sorting (置换)
- POJ Cow Sorting 置换
- [ACM] POJ 3270 Cow Sorting (置换,贪心)
- POJ 3270 Cow Sorting(置换群问题)
- poj Cow Sorting 3270 (置换群)好题
- 【POJ 3270】Cow Sorting(置换群排序)
- POJ 3270 Cow Sorting(置换最小费用)
- POJ 3270 Cow Sorting(置换循环节)
- poj 3270 Cow Sorting(置换群)
- poj-3270-Cow Sorting-置换群
- POJ 3270 Cow Sorting 置换应用
- poj 3270 Cow Sorting (置换群)
- 栈(Stack)、队列(Queue)与包(Bag)的实现
- 贪心+数学——2016 (湖南省第十二届大学生计算机程序设计竞赛 A)
- LeetCode:384. Shuffle an Array、随机数生成(C++)
- tomcat源码:系统架构
- 单点登录SSO(cookie和ticket)实现
- poj 3270 Cow Sorting(置换)
- sql 的各个版本符号的意思
- 丑数
- 利用ehcache2.6实现数据缓存
- hadoop SecondNamenode详解
- 只为比昨天强一点点
- CSU 1810 Reverse
- 第五章——指针与数组
- tomcat源码:Connector模块