题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<stdio.h> #include<string.h> #include<algorithm> #define N 501000 using namespace std; int a[N], b[N]; __int64 cnt; void Merge(int r, int mid, i
最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #include<cstdio> using namespace std; const int N=5005; int n,a[N],f[N],ans; int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') { if(
之前一直是用二分 但是因为比较难理解,写的时候也容易忘记怎么写. 今天比赛讲评的时候讲了一种用树状数组求LIS的方法 (1)好理解,自然也好写(但代码量比二分的大) (2)扩展性强.这个解法顺带求出以i为结尾的LIS,而很多题要用到这个数组来做 而二分的做法求得是当前长度下的最小值,不容易拓展. #include<bits/stdc++.h> #define REP(i, a, b) for(register int i = (a); i < (b); i++) #define _for
题目:http://poj.org/problem?id=1631 求LIS即可,我使用了树状数组. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,p,a[40005],f[40005]; int query(int x) { int ret=0; for(;x;x-=x&-x) ret=max(ret,f[x]); return re
题是水题,学习一下用树状数组求LIS. 先离散化一下,注意去重:然后就把a[i]作为下标,dp[i]作为值,max作为维护的运算插进树状数组即可. 如果是上升子序列,询问(a[i] - 1):如果是不下降子序列,询问(a[i]). ; int n, m, a[maxn], b[maxn], dp[maxn], f[maxn]; void Modify(int x, int val) { for (; x <= m; x += x&-x) f[x] = max(f[x], val); } in
http://acm.hdu.edu.cn/showproblem.php?pid=1394 //hdu 题目 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..