poj1703 Lost Cows

给定集合{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
", a[i].id);
28     }
29     return 0;
30 }
View Code
原文地址:https://www.cnblogs.com/astoninfer/p/4838312.html