• HDU 1394 Minimum Inversion Number


    题目链接:https://vjudge.net/problem/HDU-1394

    题目大意:

      给定一个由 0~n-1 组成的长度为 n 序列,如果将最前面一个元素放到最后面去,就形成了一个新序列,新序列进行同样的操作可以再形成新序列,一共能形成 n 的新序列,每个序列都有一个逆序数,求其中最小的逆序数。

    分析:

      求出 a1 ~ an 的逆序数后,其余序列的逆序数可在O(1)时间内推出。

    代码如下:

      1 #pragma GCC optimize("Ofast")
      2 #include <bits/stdc++.h>
      3 using namespace std;
      4  
      5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
      6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
      7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
      8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
      9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
     10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     13  
     14 #define pr(x) cout << #x << " = " << x << "  "
     15 #define prln(x) cout << #x << " = " << x << endl
     16  
     17 #define LOWBIT(x) ((x)&(-x))
     18  
     19 #define ALL(x) x.begin(),x.end()
     20 #define INS(x) inserter(x,x.begin())
     21  
     22 #define ms0(a) memset(a,0,sizeof(a))
     23 #define msI(a) memset(a,inf,sizeof(a))
     24 #define msM(a) memset(a,-1,sizeof(a))
     25 
     26 #define MP make_pair
     27 #define PB push_back
     28 #define ft first
     29 #define sd second
     30  
     31 template<typename T1, typename T2>
     32 istream &operator>>(istream &in, pair<T1, T2> &p) {
     33     in >> p.first >> p.second;
     34     return in;
     35 }
     36  
     37 template<typename T>
     38 istream &operator>>(istream &in, vector<T> &v) {
     39     for (auto &x: v)
     40         in >> x;
     41     return in;
     42 }
     43  
     44 template<typename T1, typename T2>
     45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     46     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     47     return out;
     48 }
     49  
     50 typedef long long LL;
     51 typedef unsigned long long uLL;
     52 typedef pair< double, double > PDD;
     53 typedef pair< int, int > PII;
     54 typedef set< int > SI;
     55 typedef vector< int > VI;
     56 typedef map< int, int > MII;
     57 typedef vector< LL > VL;
     58 typedef vector< VL > VVL;
     59 const double EPS = 1e-10;
     60 const int inf = 1e9 + 9;
     61 const LL mod = 1e9 + 7;
     62 const int maxN = 5e3 + 7;
     63 const LL ONE = 1;
     64 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
     65 const LL oddBits = 0x5555555555555555;
     66 
     67 int n, ans; 
     68 int a[maxN];
     69 
     70 #define lson l , mid , rt << 1 
     71 #define rson mid + 1 , r , rt << 1 | 1
     72 
     73 struct SegmentTree{
     74     int st[maxN << 2];
     75     
     76     inline void pushUp(int rt) {
     77         st[rt] = st[rt << 1] + st[rt << 1 | 1];
     78     }
     79     
     80     inline void pushDown(int rt) { }
     81     
     82     inline void build(int l, int r, int rt) {
     83         ms0(st);
     84     }
     85     
     86     // 单点替换,a[x] = y 
     87     inline void update(int x, LL y, int l, int r, int rt) {
     88         if(l >= r) {
     89             st[rt] = y;
     90             return;
     91         }
     92         int mid = (l + r) >> 1;
     93         if(x <= mid) update(x, y, lson);
     94         else update(x, y, rson);
     95         pushUp(rt);
     96     }
     97     
     98     // 区间查询 
     99     inline LL querySum(int L, int R, int l, int r, int rt) {
    100         if(L <= l && r <= R) return st[rt];
    101         LL ret = 0;
    102         int mid = (l + r) >> 1;
    103         
    104         if(L <= mid) ret += querySum(L, R, lson);
    105         if(R > mid) ret += querySum(L, R, rson);
    106         return ret;
    107     }
    108 };
    109 SegmentTree segTr;
    110 
    111 int main(){
    112     INIT();
    113     while(cin >> n) {
    114         For(i, 1, n) {
    115             cin >> a[i];
    116             ++a[i];
    117         }
    118         ans = 0;
    119         segTr.build(1, n, 1);
    120         
    121         For(i, 1, n) {
    122             ans += segTr.querySum(a[i], n, 1, n, 1); // 计算大于a[i]且在a[i]之前出现过的数的个数 
    123             segTr.update(a[i], 1, 1, n, 1); // 标记为1,表示已经出现过 
    124         }
    125         
    126         int tmp = ans;
    127         For(i, 1, n - 1) {
    128             tmp = tmp + n - 2 * a[i] + 1;
    129             ans = min(ans, tmp);
    130         }
    131         cout << ans << endl;
    132     }
    133     return 0;
    134 }
    View Code
  • 相关阅读:
    Java三大框架
    单例模式和工厂模式(百度文库)
    使用java代码编辑oracle数据库
    extends 与implements的区别和用法
    介绍MVC编程架构模式
    接口具体是什么东西
    Servlet和JSP的本质和区别
    用户注册,登录,留言系统
    页面跳转的五种方法
    cookie的长度和限制数量
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/10795823.html
Copyright © 2020-2023  润新知