【CDOJ931】Car race game(树状数组求逆序)

题目连接:http://acm.uestc.edu.cn/#/problem/show/931

OJ评判系统有些坑,不支持__int64以及输出的%I64d大家注意。全开long long也会TLE,比较坑。逆序的基础操作题,不错。

 1 #include <bits/stdc++.h>
 2 #define MAX 100010
 3 using namespace std;
 4
 5 int s[MAX * 10];
 6
 7 struct Node {
 8     int x, v;
 9 } node[MAX];
10
11 bool cmp (Node a, Node b) {
12     if (a.x == b.x) {
13         return a.v > b.v;
14     }
15     return a.x < b.x;
16 }
17
18 int lowbit (int x) {
19     return x & (-x);
20 }
21
22 int sum (int p) {
23     int ans = 0;
24     while (p) {
25         ans += s[p];
26         p -= lowbit(p);
27     }
28     return ans;
29 }
30
31 void add (int p, int del) {
32     while (p <= 1000000) {
33         s[p] += del;
34         p += lowbit(p);
35     }
36 }
37
38 int main () {
39     int n, i;
40     while (scanf("%d", &n) != EOF) {
41         long long ans = 0;
42         for (i = 0; i < n; ++ i) {
43             scanf("%d %d", &node[i].x, &node[i].v);
44         }
45         sort (node, node + n, cmp);
46         memset(s, 0, sizeof(s));
47         for (i = 0; i < n; ++ i) {
48             ans = ans + i - sum(node[i].v);
49             add(node[i].v, 1);
50         }
51         printf ("%lld\n", ans);
52     }
53     return 0;
54 }
时间: 2024-10-07 05:16:10

【CDOJ931】Car race game(树状数组求逆序)的相关文章

poj2299(离散化+树状数组求逆序)

数据范围比较大,先用离散化将数据映射到可控的范围,然后应用树状数组求逆序求解. 总共有N个数,如何判断第i+1个数到最后一个数之间有多少个数小于第i个数呢?不妨假设有一个区间 [1,N],只需要判断区间[i+1,N]之间有多少个数小于第i个数.如果我们把总区间初始化为0,然后把第i个数之前出现过的数都在相应的区间把它的值定为1,那么问题就转换成了[i+1,N]值的总和.再仔细想一下,区间[1,i]的值+区间[i+1,N]的值=区间[1,N]的值(i已经标记为1),所以区间[i+1,N]值的总和等

POJ-2299 Ultra-QuickSort(用树状数组求逆序对数)

题目链接 ac代码 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime> #include&

树状数组求逆序对

给定n个数,要求这些数构成的逆序对的个数.除了用归并排序来求逆序对个数,还可以使用树状数组来求解.树状数组求解的思路:开一个能大小为这些数的最大值的树状数组,并全部置0.从头到尾读入这些数,每读入一个数就更新树状数组,查看它前面比它小的已出现过的有多少个数sum,然后用当前位置减去该sum,就可以得到当前数导致的逆序对数了.把所有的加起来就是总的逆序对数.题目中的数都是独一无二的,这些数最大值不超过999999999,但n最大只是500000.如果采用上面的思想,必然会导致空间的巨大浪费,而且由

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

题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in

ZOJ-2386 Ultra-QuickSort 【树状数组求逆序数+离散化】

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input seque

树状数组求逆序数

poj 2299 树状数组求逆序数题目链接:http://poj.org/problem?id=2299 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的