• POJ 2299 Ultra-QuickSort


    题目链接:https://vjudge.net/problem/POJ-2299

    题目大意

      归并排序求逆序数经典题。

    分析

      略。

    代码如下

      1 #include <cmath>
      2 #include <ctime>
      3 #include <iostream>
      4 #include <string>
      5 #include <vector>
      6 #include <cstdio>
      7 #include <cstdlib>
      8 #include <cstring>
      9 #include <queue>
     10 #include <map>
     11 #include <set>
     12 #include <algorithm>
     13 #include <cctype>
     14 #include <stack>
     15 #include <deque>
     16 #include <list>
     17 #include <sstream>
     18 #include <cassert>
     19 using namespace std;
     20  
     21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     22 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
     23 #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
     24 #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
     25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
     26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
     27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
     28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
     29  
     30 #define pr(x) cout << #x << " = " << x << "  "
     31 #define prln(x) cout << #x << " = " << x << endl
     32  
     33 #define LOWBIT(x) ((x)&(-x))
     34  
     35 #define ALL(x) x.begin(),x.end()
     36 #define INS(x) inserter(x,x.begin())
     37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
     38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
     39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
     40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
     41  
     42 #define ms0(a) memset(a,0,sizeof(a))
     43 #define msI(a) memset(a,0x3f,sizeof(a))
     44 #define msM(a) memset(a,-1,sizeof(a))
     45 
     46 #define MP make_pair
     47 #define PB push_back
     48 #define ft first
     49 #define sd second
     50  
     51 template<typename T1, typename T2>
     52 istream &operator>>(istream &in, pair<T1, T2> &p) {
     53     in >> p.first >> p.second;
     54     return in;
     55 }
     56  
     57 template<typename T>
     58 istream &operator>>(istream &in, vector<T> &v) {
     59     for (auto &x: v)
     60         in >> x;
     61     return in;
     62 }
     63 
     64 template<typename T>
     65 ostream &operator<<(ostream &out, vector<T> &v) {
     66     Rep(i, v.size()) out << v[i] << " 
    "[i == v.size()];
     67     return out;
     68 }
     69  
     70 template<typename T1, typename T2>
     71 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
     72     out << "[" << p.first << ", " << p.second << "]" << "
    ";
     73     return out;
     74 }
     75 
     76 inline int gc(){
     77     static const int BUF = 1e7;
     78     static char buf[BUF], *bg = buf + BUF, *ed = bg;
     79     
     80     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
     81     return *bg++;
     82 } 
     83 
     84 inline int ri(){
     85     int x = 0, f = 1, c = gc();
     86     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
     87     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
     88     return x*f;
     89 }
     90 
     91 template<class T>
     92 inline string toString(T x) {
     93     ostringstream sout;
     94     sout << x;
     95     return sout.str();
     96 }
     97 
     98 inline int toInt(string s) {
     99     int v;
    100     istringstream sin(s);
    101     sin >> v;
    102     return v;
    103 }
    104 
    105 //min <= aim <= max
    106 template<typename T>
    107 inline bool BETWEEN(const T aim, const T min, const T max) {
    108     return min <= aim && aim <= max;
    109 }
    110  
    111 typedef long long LL;
    112 typedef unsigned long long uLL;
    113 typedef vector< int > VI;
    114 typedef vector< bool > VB;
    115 typedef vector< char > VC;
    116 typedef vector< double > VD;
    117 typedef vector< string > VS;
    118 typedef vector< LL > VL;
    119 typedef vector< VI > VVI;
    120 typedef vector< VB > VVB;
    121 typedef vector< VS > VVS;
    122 typedef vector< VL > VVL;
    123 typedef vector< VVI > VVVI;
    124 typedef vector< VVL > VVVL;
    125 typedef pair< int, int > PII;
    126 typedef pair< LL, LL > PLL;
    127 typedef pair< int, string > PIS;
    128 typedef pair< string, int > PSI;
    129 typedef pair< string, string > PSS;
    130 typedef pair< double, double > PDD;
    131 typedef vector< PII > VPII;
    132 typedef vector< PLL > VPLL;
    133 typedef vector< VPII > VVPII;
    134 typedef vector< VPLL > VVPLL;
    135 typedef vector< VS > VVS;
    136 typedef map< int, int > MII;
    137 //typedef unordered_map< int, int > uMII;
    138 typedef map< LL, LL > MLL;
    139 typedef map< string, int > MSI;
    140 typedef map< int, string > MIS;
    141 typedef set< int > SI;
    142 typedef stack< int > SKI;
    143 typedef queue< int > QI;
    144 typedef priority_queue< int > PQIMax;
    145 typedef priority_queue< int, VI, greater< int > > PQIMin;
    146 const double EPS = 1e-8;
    147 const LL inf = 0x7fffffff;
    148 const LL infLL = 0x7fffffffffffffffLL;
    149 const LL mod = 1e9 + 7;
    150 const int maxN = 5e5 + 7;
    151 const LL ONE = 1;
    152 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
    153 const LL oddBits = 0x5555555555555555;
    154 
    155 int a[maxN], b[maxN], N;
    156 LL ans;
    157 
    158 void Merge(int l, int mid, int r) {
    159     int i = l, j = mid + 1, k = l;
    160     while(i <= mid && j <= r) {
    161         if(a[i] <= a[j]) b[k++] = a[i++];
    162         else {
    163             b[k++] = a[j++];
    164             ans += (LL)(mid - i + 1);
    165         }   
    166     }
    167     while(i <= mid) b[k++] = a[i++];
    168     while(j <= r) b[k++] = a[j++];
    169     For(h, l, r) a[h] = b[h];
    170 }
    171 
    172 void MergeSort(int l, int r) {
    173     if(l >= r) return;
    174     int mid = (l + r) >> 1;
    175     
    176     MergeSort(l, mid);
    177     MergeSort(mid + 1, r);
    178     Merge(l, mid, r);
    179 }
    180 
    181 int main(){
    182     //freopen("MyOutput.txt","w",stdout);
    183     //freopen("input.txt","r",stdin);
    184     //INIT();
    185     while(scanf("%d", &N) && N)    {
    186         ans = 0;
    187         Rep(i, N) scanf("%d", &a[i]);
    188         MergeSort(0, N - 1);
    189         printf("%lld
    ", ans);
    190     }
    191     return 0;
    192 }
    View Code
  • 相关阅读:
    在cmd中登录ftp服务器
    Is valid identifier?
    Hosting Your Own NuGet Feeds
    NuGet学习笔记
    软件版本的处理
    有些事明显对自己有益,为什么却无法去做?
    .net 程序员成长路线图?
    正确跑步姿势是怎样的?
    什么叫做内心强大?怎样变成一个内心强大的人?
    使用命令行复制目录
  • 原文地址:https://www.cnblogs.com/zaq19970105/p/11333954.html
Copyright © 2020-2023  润新知