fzu 2184 逆序数还原(vector)

来源:互联网 发布:淘宝伴侣 编辑:程序博客网 时间:2024/05/19 02:03

解析:

5
2 0 1 0 0
vector中初始化1~n的数字。
比如上面一个要还原的逆序数,开头是2那么就是求出1~n中第3小的数字,就是3,把3从vector中删除;然后是0,就找到vector中第1小的数字,把1删除;然后是1,找到vector中第2小的数字,把2删除……
把每次找到的数字保存就是最终答案。

AC代码

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <vector>using namespace std;typedef long long ll;const int N = 1e3 + 20;vector<int> vec;int ans[N];int main() {    int n;    while(scanf("%d", &n) != EOF) {        vec.clear();        for(int i = 1; i <= n; i++) {            vec.push_back(i);        }        int x;        vector<int>::iterator it;        for(int i = 1; i <= n; i++) {            scanf("%d", &x);            ans[i] = vec[x];            it = vec.begin() + x;            vec.erase(it);        }        printf("%d", ans[1]);        for(int i = 2; i <= n; i++) {            printf(" %d", ans[i]);        }        puts("");    }    return 0;}
0 0