↓ Skip to main content
  1. Posts/

poj 2299 Ultra-QuickSort (树状数组+离散化)

·591 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

这道题可以总结的地方不少。

1:对于一组乱序数列,每次只能交换相邻元素,达到有序交换的次数就是原数列中你逆序对的个数。

  cf上好像总喜欢出这个题。。。我印象中就出现三次了。。。。。

2:原始数组a[i]和树状数组的t[i]的对应问题(???存在疑问。。。应该只是这道题,而不是一般规律!)

  这道题n是500000,如果直接开数组是可以开得下的,不需要离散化。**但是树状数组的下标对应的是原始数组的值!**也就是t[i]的下表最大可能为999,999,999 ! 显然存不下,需要离散化。

**3:学习了离散化的又一种写法。 **

代码实现
 1/*************************************************************************
 2	> File Name: code/poj/2299.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年08月04日 星期二 12时27分32秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x7fffffff;
31const int N=5E5+7;
32int n;
33int t[N];
34int ref[N];
35
36struct Q
37{
38    int val,id;
39}q[N];
40
41
42bool cmp(Q a,Q b)
43{
44    if (a.val<b.val) return true;
45    return false;
46}
47int lowbit( int x)
48{
49    return x&(-x);
50}
51void update ( int x,int c)
52{
53    for ( int i = x ; i < N ; i = i + lowbit(i))
54    {
55	t[i] = t[i] + c;
56    }
57}
58LL Sum( int x)
59{
60    LL res = 0;
61    for ( int i = x; i >= 1 ; i = i - lowbit(i))
62    {
63	res = res + t[i];
64    }
65    return res;
66}
67int main()
68{
69    while (~scanf("%d",&n)&&n)
70    {
71	memset(t,0,sizeof(t));
72	for ( int i = 1 ; i  <= n ; i++ )
73	{
74	    scanf("%d",&q[i].val);  //离散化的时候相对大小不能打乱
75	    q[i].id = i;
76	}
77	sort(q+1,q+n+1,cmp);
78	for ( int i = 1 ; i <= n ; i++ )
79	{
80	    ref[q[i].id] = i;
81	}
82	LL ans = 0;
83	for ( int i = 1 ; i <= n ; i++ )
84	{
85	    update(ref[i],1);
86	    ans = ans + i-Sum(ref[i]);
87	  //  cout<<"ans:"<<ans<<endl;
88
89	}
90	cout<<ans<<endl;
91    }
92	return 0;
93}

Related

poj 2481 Cows(树状数组||线段树)

·1589 words·4 mins
poj 2481 题目链接 题意:给定n个区间,问对于每个区间,有多少个区间真包含该区间(真包含的意思是说,两个区间不能完全重合) 思路: 下面是一年前用树状数组过掉的时候写的题解: 和 star那道题差不多。

hdu 4022 Bombing (离散化)

·348 words·1 min
wa了两次,原因是在同一个点可能有多个基地。。。 所以用set 是错误的,应该用multiset 然后因为这道题看到了map+set实现离散化的另外一种写法 我的代码: 代码实现 1/************************************************************************* 2 > File Name: code/hdoj/4022.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年08月01日 星期六 04时37分20秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#define y0 abc111qqz 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define tm crazy111qqz 25#define lr dying111qqz 26using namespace std; 27#define REP(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef unsigned long long ULL; 30const int N=2E5+7; 31const int inf = 0x7fffffff; 32 33map<int,int>xmap,ymap; 34multiset<int>x[N]; 35multiset<int>y[N]; 36int main() 37{ 38 int n,m; 39 while (scanf("%d %d",&n,&m)!=EOF) 40 { 41 if (n==0&&m==0) break; 42 for ( int i = 1 ; i <= n ; i++) 43 { 44 x[i].clear(); 45 y[i].clear(); 46 } 47 xmap.clear(); 48 ymap.clear(); 49 int tx,ty; 50 int cntx=0,cnty=0; 51 for ( int i = 1 ; i <= n ; i++ ) 52 { 53 scanf("%d %d",&tx,&ty); 54 if (!xmap[tx]) xmap[tx]=++cntx; 55 if (!ymap[ty]) ymap[ty]=++cnty; 56 x[xmap[tx]].insert(ymap[ty]); 57 y[ymap[ty]].insert(xmap[tx]); 58 } 59 int c,d; 60 set<int>::iterator it; 61 for ( int i = 1; i <= m ; i++ ) 62 { 63 scanf("%d %d",&c,&d); 64 if (c==0) 65 { 66 cout<<x[xmap[d]].size()<<endl; 67 for ( it = x[xmap[d]].begin();it!=x[xmap[d]].end();it++) 68 { 69 y[*it].erase(xmap[d]); 70 } 71 x[xmap[d]].clear(); 72 } 73 else 74 { 75 cout<<y[ymap[d]].size()<<endl; 76 for ( it =y[ymap[d]].begin();it!=y[ymap[d]].end();it++) 77 { 78 x[*it].erase(ymap[d]); 79 80 } 81 y[ymap[d]].clear(); 82 } 83 } 84 printf("\n"); 85 } 86 87 return 0; 88}

hdu 5233 Gunner II (bc #42 B) (离散化)

·872 words·2 mins
Gunner II # **Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1433 Accepted Submission(s): 540 ** Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n trees. The i-th bird stands on the top of the i-th tree. The trees stand in straight line from left to the right. Every tree has its height. Jack stands on the left side of the left most tree. When Jack shots a bullet in height H to the right, the nearest bird which stands in the tree with height H will falls.