摘要: 求逆序数的方法有很多,比如归并排序,但本文重点讲一下如何用树状数组来求逆序数。 当数据的范围较小时,比如maxn=100000,那么我们可以开一个数组c[maxn],来记录前面数据的出现情况,初始化为0;当数据a出现时,就令c[a]=1。这样的话, 欲求某个数a的逆序数,只需要算出在当前状态下c[a+1,maxn]中有多少个1,因为这些位置的数在a之前出现且比a大。但是若每添加一个数据a时,就得从a+1到 maxn搜一遍,复杂度太高了。树状数组却能很好的解决这个问题,同样开一个数组d[maxn],初始化为0,d[i]记录下i结点所管辖范围内当前状态有多少个数;当添加数 ... 阅读全文
posted @ 2013-08-16 21:45 EtheGreat 阅读(2850) 评论(2) 推荐(1) 编辑
摘要: 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003分析:定义dp[i][1]表示到第i个数时的最大连续和,dp[i][1]表示dp[i][0]取到最大值时的左边起始位。Max Sum 1 #include 2 #include 3 #include 4 #include 5 #define maxn 100005 6 using namespace std; 7 int num[maxn],dp[maxn][2]; 8 int main() 9 {10 int T,cas=1;11 scanf("%d",&T);1 阅读全文
posted @ 2013-08-16 20:43 EtheGreat 阅读(182) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010分析:dfs+奇偶剪枝。Tempter of the Bone 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 char s[10][10]; 7 bool visit[10][10],flag; 8 int dx[4]={0,0,1,-1}; 9 int dy[4]={-1,1,0,0};10 int n,m,t,di,dj;11 void dfs(int time,int i,int j)12 {13 阅读全文
posted @ 2013-08-16 20:33 EtheGreat 阅读(214) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://poj.org/problem?id=2488分析:如果存在合法路径,那么一定可以将路径的起始点定为(A,1),从该点按字典序DFS,如果找到一条路径,那么这条路即为所求。 1 #include 2 #include 3 #include 4 #include 5 #define maxn 27 6 using namespace std; 7 int dirx[8]={-2,-2,-1,-1,1,1,2,2}; 8 int diry[8]={-1,1,-2,2,-2,2,-1,1}; 9 char path[maxn0&&ny>0&&am 阅读全文
posted @ 2013-08-09 19:22 EtheGreat 阅读(222) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://poj.org/problem?id=2406分析:求最小周期。 1 #include 2 #include 3 #include 4 #include 5 #define maxn 1000005 6 using namespace std; 7 char s[maxn]; 8 int next[maxn],len; 9 void get_next()10 {11 int i=0,j=-1;len=strlen(s);12 next[0]=-1;13 while(i<len)14 {15 if(j==-1||s[i]==... 阅读全文
posted @ 2013-08-09 09:21 EtheGreat 阅读(223) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://poj.org/problem?id=2752分析:no! 1 #include 2 #include 3 #include 4 #define maxn 400005 5 using namespace std; 6 char s[maxn]; 7 int len,next[maxn],ans[maxn]; 8 void get_next() 9 {10 int i=0,j=-1;len=strlen(s);11 next[0]=-1;12 while(i=1;j--)31 printf("%d ",ans[j]);32 ... 阅读全文
posted @ 2013-08-09 01:09 EtheGreat 阅读(151) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://poj.org/problem?id=3461分析:求一个串在另一个串中出现的次数,显然用KMP可以解决。 1 #include 2 #include 3 #include 4 #define maxn1 10005 5 #define maxn2 1000005 6 using namespace std; 7 char s[maxn1],t[maxn2]; 8 int next[maxn1],sum,len1,len2; 9 void get_next()10 {11 int i,j;len1=strlen(s);12 next[0]=-1;13 ... 阅读全文
posted @ 2013-08-09 00:21 EtheGreat 阅读(217) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://acm.uestc.edu.cn/problem.php?pid=1404分析:定义dp[i][j]表示i位时最左边为j时的情况,那么dp[i][[j]可以由dp[i-1][k](k>=j)得到。Non-Decreasing Digits#include#include#include#include#define maxn 100005#define ll long longusing namespace std;ll dp[70][12];void solve(){ for(int i=0;i=0;j--) { dp[i][j]=sum+dp[i-1][j]; 阅读全文
posted @ 2013-08-01 21:59 EtheGreat 阅读(190) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://acm.uestc.edu.cn/problems.php?vol=15分析:首先筛出sqrt(2^31-1)以内的素数,对于给定的区间[L,R],仍然用筛素数的思想把那些是前面筛选出来的素数的倍数的做标记,然后从左到右扫一遍即可。How many primes 1 #include 2 #include 3 #include 4 #include 5 #define maxn 100005 6 using namespace std; 7 int prime[maxn],t; 8 bool ans[1000005]; 9 bool flag[maxn]={false} 阅读全文
posted @ 2013-08-01 20:22 EtheGreat 阅读(215) 评论(0) 推荐(0) 编辑
摘要: 原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1073分析:dp,dp[i]表示钱为i且恰好用完时能买的最少土地数,易知dp[i]=min(i,dp[i-j*j]+1)(1#include#include#include#include#include#include#include#define ll long long#define maxn 60005using namespace std;int dp[maxn];void solve(){ memset(dp,0,sizeof(dp)); dp[0]=0;dp[1]... 阅读全文
posted @ 2013-07-31 23:35 EtheGreat 阅读(184) 评论(0) 推荐(0) 编辑