码迷,mamicode.com
首页 > 其他好文 > 详细

poj1703 Lost Cows

时间:2015-09-25 16:27:38      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

给定集合{1,2,...,n}的一个置换,指定每个位置上在其左方且比其小的数的个数,求该置换。

这题我目前还只会O(n^2)的做法。

以后再用更高效的算法解决。

 

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

 

 

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 8e3 + 10;
 6 int n;
 7 struct Point{
 8     int id, idx;
 9 }a[maxn];
10 
11 bool cmp(Point x, Point y) { return x.idx < y.idx; }
12 
13 int main(){
14     freopen("in.txt", "r", stdin);
15     while(~scanf("%d", &n)){
16         for(int i = 1; i <= n; i++) a[i].id = i;
17         a[1].idx = 1;
18         for(int i = 2, t; i <= n; i++){
19             scanf("%d", &t);
20             if(t + 1 == i) a[i].idx = i;
21             else{
22                 for(int j = i; j > t + 1; j--) a[j].idx = a[j - 1].idx;
23                 a[t + 1].idx = i;
24             }
25         }
26         sort(a + 1, a + n + 1, cmp);
27         for(int i = 1; i <= n; i++) printf("%d\n", a[i].id);
28     }
29     return 0;
30 }
View Code

 

poj1703 Lost Cows

标签:

原文地址:http://www.cnblogs.com/astoninfer/p/4838312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!