2012年8月12日

摘要: pku2182:http://poj.org/problem?id=2182题意:给出n个数(1~n),接下来第2~n行输出n-1个数,第i个数num[i]表示原序列的第i个位置的数ans[i]前面的数中比他小的个数,求原序列。解法:线段树:先建树,用tot标记该线段中未确定的数的个数,用len表示线段长度,按所给的num从后往前推code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;const int maxn 阅读全文
posted @ 2012-08-12 23:01 acmer-jun 阅读(176) 评论(0) 推荐(0) 编辑

2012年8月9日

摘要: hoj2275:http://acm.hit.edu.cn/hoj/problem/view?id=2275题意:给一个数列,问有多少组满足:三个数Ai < Aj > Ak 且 i < j < k.解法:树状数组:用数组left存储所有比左边的数小的数的个数,则结果为left值*(所有比右边的数小的数的个数)code:#include<iostream>#include<cstdlib>#include<cstdio>int num[50010],c[50010],left[50010];long long s;int lowbit( 阅读全文
posted @ 2012-08-09 14:39 acmer-jun 阅读(181) 评论(0) 推荐(0) 编辑

2012年8月5日

摘要: hoj1867:http://acm.hit.edu.cn/hoj/problem/view?id=1867题意:c个连锁店,n条指令,每间店的初始商品数量。有两种指令:x,y,z,若x=0,表示第y个店的数目增加z,若x=1,表示查询店y到z中商品数量为素数的个数解法:树状数组code:#include<iostream>#include<cstdio>#include<cstdlib>const int maxn=1000010;int a[maxn],v[maxn]; //a为商品数目,v为素数个数int prime(int x) //判断素... 阅读全文
posted @ 2012-08-05 22:37 acmer-jun 阅读(254) 评论(0) 推荐(0) 编辑
摘要: pku2352: http://poj.org/problem?id=2352题意:给出星星的坐标,问每一层次n有多少个星星:星星a的左边或下边共有x个星星,则a属于层次x,ans[x]++,求ans[0]~ans[n-1]解法1:线段树:从给出的坐标的顺序可知后面的坐标不会影响的前面的坐标,所以可以边输入边计算。首先建树,再从点1开始往下搜,若所求点d在树的左子树,继续往下搜,若在树的右子树,加上左子树的数目(左子树的点都小于d),再搜右子树,遇到左端点a与右端点b相同的情况,此时a=b=d,更新点,加一。code1:#include<iostream>#include<c 阅读全文
posted @ 2012-08-05 11:41 acmer-jun 阅读(185) 评论(0) 推荐(0) 编辑

2012年8月1日

摘要: pku2299:http://poj.org/problem?id=2299题意:给出n个数,问要将其按从小到大排序至少需要多少步骤。解法:归并排序:将待排序集合一分为二,直至待排序集合只剩下一个元素为止,然后不断合并两个排好序的数组段。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;int d[500005],c[500005];__int64 ans;void msort(int l,int m,int 阅读全文
posted @ 2012-08-01 23:11 acmer-jun 阅读(225) 评论(0) 推荐(0) 编辑

2012年7月31日

摘要: pku3468: http://poj.org/problem?id=3468题意:给定一段区间,其初始数值给定,Q [a,b]表示求区间[a,b]的和,C [a,b]c表示将区间[a,b]的所有数值加上c解法:线段树lazy思想:由于整段要更新,所以每次只需更新父节点,并用lazy标记,当遇到lazy!=0时,更新下一代,同时标记,这样一直往下更新。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>u 阅读全文
posted @ 2012-07-31 11:36 acmer-jun 阅读(212) 评论(0) 推荐(0) 编辑

2012年7月30日

摘要: hdu1166: http://acm.hdu.edu.cn/showproblem.php?pid=1166题意:给出k个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人,接下来有一些命令(增加、减少、查询、结束),输出每次查询结果。解法:线段树code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;const int inf=1<<29;st 阅读全文
posted @ 2012-07-30 16:56 acmer-jun 阅读(161) 评论(0) 推荐(0) 编辑

2012年7月26日

摘要: 开关灯问题:有n个人和n盏灯,第一个人开所有的灯,第二个人按所有2的倍数的灯的开关,第三个人按所有3的倍数的灯的开关,依此类推,求最后多少盏灯亮着解法:按奇数次则亮,偶数次则灭,所以因子数为奇数亮,所以平方数亮,所以即求n内平方数个数,为(int)sqrt(n),如n=16,根号16=4,所以平方数有1*1,2*2,3*3,4*4,即1、4、9、16四个。code:#include<stdio.h>#include<math.h>int main(){ int t,n,m; scanf("%d",&t); while(t--) { scanf 阅读全文
posted @ 2012-07-26 12:24 acmer-jun 阅读(176) 评论(0) 推荐(0) 编辑
摘要: 题意:求1-n内与n互质的数的个数code:#include<iostream>#include<cstdio>#include<cstdlib>int phi[10000];int main(){ int n,i,cnt,j,s; while(scanf("%d",&n)!=EOF) { cnt=0;s=n; for(i=2;i*i<=n;i++) //求质因数 { if(n%i==0) { phi[cnt++]=i; whil... 阅读全文
posted @ 2012-07-26 12:23 acmer-jun 阅读(184) 评论(0) 推荐(0) 编辑
摘要: 题意:求1<=x<=n内不超过x的所有与x互质的数的个数解法:欧拉公式code:#include<iostream>#include<cstdio>#include<cstdlib> const int N=100; int phi[N],prime[N];int main() { int i,j; prime[0]=prime[1]=0; for(i=2;i<N;i++) { prime[i]=1; } for(i=2;i*i<N;i++) { if(prime[i]) { ... 阅读全文
posted @ 2012-07-26 12:22 acmer-jun 阅读(2161) 评论(0) 推荐(0) 编辑

导航