UVALive 6508 Permutation Graphs

Permutation Graphs

Time Limit: 3000ms

Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 6508
64-bit integer IO format: %lld      Java class name: Main

解题:逆序数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 100010;
 5 int c[maxn],hhash[maxn],n;
 6 void add(int i){
 7     while(i > 0){
 8         c[i] += 1;
 9         i -= i&-i;
10     }
11 }
12 int sum(int i,int ret = 0){
13     while(i < maxn){
14         ret += c[i];
15         i += i&-i;
16     }
17     return ret;
18 }
19 int main(){
20     int kase,tmp;
21     scanf("%d",&kase);
22     while(kase--){
23         scanf("%d",&n);
24         for(int i = 1; i <= n; ++i){
25             scanf("%d",&tmp);
26             hhash[tmp] = i;
27         }
28         memset(c,0,sizeof c);
29         LL ret = 0;
30         for(int i = 0; i < n; ++i){
31             scanf("%d",&tmp);
32             ret += sum(hhash[tmp] + 1);
33             add(hhash[tmp]);
34         }
35         printf("%lld\n",ret);
36     }
37     return 0;
38 }

时间: 2024-12-17 18:01:06

UVALive 6508 Permutation Graphs的相关文章

逆序数 UVALive 6508 Permutation Graphs

题目传送门 1 /* 2 题意:给了两行的数字,相同的数字连线,问中间交点的个数 3 逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数 4 用归并排序或线段树求.想到了就简单了:) 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 #include <vector> 11 us

Regionals 2013 Asia - Daejeon (部分题目题解)

题目链接:Regionals 2013 Asia - Daejeon 6500 Boxes 题意:将箱子(矩阵的1)全移动到矩阵的底部需要几步 思路:按列从下到上统计.(n,m)的矩阵,移动一个箱子(x,y),如果有c个箱子在底部,那么移动该箱子的步数是(n-x-c-1). AC代码: #include <stdio.h> #include <string.h> int mp[110][110]; int main() { int t; int i,j,n,m; scanf(&qu

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 5971

Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it doesn't have x and x+1 appearing consecutively, where (1 ≤ x < N)  For example, for N=3 , all goodpermutations are: 1. {1, 3, 2} 2.{2, 1, 3} 3.{3, 2,

UVALive - 3263 - That Nice Euler Circuit (计算几何~~)

UVALive - 3263 That Nice Euler Circuit Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his primary sch

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

Representations of graphs

We can choose between two standard ways to represent a graph as a collection of adjacency lists or as an adjacency matrix. Either way appliesto both directed and undirected graphs. Because the adjacency-list representationprovides a compact way to re

31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

Permutation Sequence

The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3