2012年7月25日

摘要: pku1178: http://poj.org/problem?id=1178题意:给出一行字符串,每对字母+数字表示棋盘上的一点,如A表示横坐标为1,B表示横坐标为2等,第一对表示王,只可上下左右移动,后面每一对表示一个马,走法跟象棋一样(没有算马脚),若马与王到了同一个格子,马可以带着王走,求总的最少需要多少步可使所有马和王汇集到一个格子里解法:floyd+3个枚举:先用floyd算出马在每个点到其他点的最小步数,再3层枚举:第一层枚举所有成员聚集点,第二层枚举马接王的点,第三层枚举求出马接王再到达聚集点中需要步数最小的马code:#include<iostream>#incl 阅读全文
posted @ 2012-07-25 22:07 acmer-jun 阅读(178) 评论(0) 推荐(0) 编辑
摘要: pku1125: http://poj.org/problem?id=1125题意:群发短信:给出n个群发者,输出的第i行的第一个输出k表示第i个发信息者可以给k个人发短信,接着输出k对数据,每对两个数分别表示被联系者的编号和与其联系时间,问由哪个人群发短信花费的时间最短,转化为最短路问题即为:给出每个点到其他点的距离,求所有点到其他点最长距离的最小值,并输出该起点解法:floyd求出每个点到各点的最短路,再枚举各点,求出各个最大值,再取最小值code:#include<iostream>#include<cstdio>#include<cstdlib>in 阅读全文
posted @ 2012-07-25 22:02 acmer-jun 阅读(179) 评论(0) 推荐(0) 编辑
摘要: hdu2141: http://acm.hdu.edu.cn/showproblem.php?pid=2141题意:给出3个数列a、b、c,再给出s个x,问是否存在a[i]+b[j]+c[k]=x解法:先让a+b,得到和数列sum,并排序,再给c排序,再二分搜sum中是否存在sum[i]=x-c[j]code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;bool cmp(int a,int b){ return 阅读全文
posted @ 2012-07-25 21:50 acmer-jun 阅读(149) 评论(0) 推荐(0) 编辑
摘要: hdu2272: http://acm.hdu.edu.cn/showproblem.php?pid=2272题意:给出n个人的预期排名,问怎样的排名使得所有人的实际排名与预期排名之差的绝对值的和最小贪心法:将预期排名从小到大排序,再从1~n名依次给他们排名,则所求值最小。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>using namespace std;struct str{ char s[30]; 阅读全文
posted @ 2012-07-25 21:41 acmer-jun 阅读(172) 评论(0) 推荐(0) 编辑
摘要: hdu2117: http://acm.hdu.edu.cn/showproblem.php?pid=2117题意:输入n和m(1<=n<=10^7,1<=m<=10^5),求1/n小数点后第m个数解法:模拟除法。code:#include<iostream>#include<cstdio>#include<cstdlib>int main(){ int m,n,s,x; while(scanf("%d%d",&n,&m)!=EOF) { s=1; for(int i=0;i<=m;i++) 阅读全文
posted @ 2012-07-25 21:35 acmer-jun 阅读(138) 评论(0) 推荐(0) 编辑
摘要: hdu2116: http://acm.hdu.edu.cn/showproblem.php?pid=2116题意:输入k(2<=k<=64),再输入x,y(-2^(k-1)<=x,y<=2^(k-1)-1),判断x+y是否溢出(即是否超出[-2^(k-1),2^(k-1)-1])解法:若直接相加可能溢出,所以用max-x>y和min-x<y来判断.code:#include<iostream>#include<cstdio>#include<cstdlib>int main(){ long long int x,y,ma 阅读全文
posted @ 2012-07-25 21:32 acmer-jun 阅读(197) 评论(0) 推荐(0) 编辑
摘要: hdu2115: http://acm.hdu.edu.cn/showproblem.php?pid=2115题意:输入n组名字和对应的时间(分:秒),要求按时间长度由短到长排序,并输出对应排名,若时间一样,则按名字字典序排序,名次可以并列。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;struct name{ char v[200],w[200]; int 阅读全文
posted @ 2012-07-25 21:27 acmer-jun 阅读(136) 评论(0) 推荐(0) 编辑
摘要: hdu2114: http://acm.hdu.edu.cn/showproblem.php?pid=2114题意:求x=1^3+2^3+3^3+……+n^3结果的后四位。解法:原式x=((n*(n+1))/2)^2,求后四位,即求x%10000,由公式(a*b)%m=((a%m)*(b%m))%m,(a/2)%m=(a%m)/2可求。code:#include<iostream>#include<cstdio>#include<cstdlib>int n;int main(){ int s; while(scanf("%d",& 阅读全文
posted @ 2012-07-25 21:21 acmer-jun 阅读(150) 评论(0) 推荐(0) 编辑
摘要: hdu2138: http://acm.hdu.edu.cn/showproblem.php?pid=2138题意:给出n个数,求素数个数解法:为了不超时,加速法:求出65535内的素数,对于65535以后的数,若不能被这些素数整除,则为素数。证明如下:对于大于65535的整形非素数,有两种情况,一种是至少有一个因子在65535内,另一种是所有因子都在65535内,但是65536*65535>int,所以第二种不可能。code:#include<iostream>#include<cstdio>#include<cstdlib>int v[65536] 阅读全文
posted @ 2012-07-25 21:14 acmer-jun 阅读(155) 评论(0) 推荐(0) 编辑
摘要: hdu2135: http://acm.hdu.edu.cn/showproblem.php?pid=2135题意:旋转一个n*n的矩阵,m为负代表逆时针,m为正代表顺时针,输出旋转后的矩阵code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>char v[20][20];int main(){ int m,n; while(scanf("%d%d",&n,&m)!=EOF) { getchar(); for(int i=0 阅读全文
posted @ 2012-07-25 21:08 acmer-jun 阅读(148) 评论(0) 推荐(0) 编辑
摘要: hdu2133: http://acm.hdu.edu.cn/showproblem.php?pid=2133题意(解法):已知0年1月1日是周6,输入一个日期,问星期几code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>int tianshu1[]={31,28,31,30,31,30,31,31,30,31,30,31};int tianshu2[]={31,29,31,30,31,30,31,31,30,31,30,31};char tab[7][ 阅读全文
posted @ 2012-07-25 21:04 acmer-jun 阅读(182) 评论(0) 推荐(0) 编辑
摘要: hdu2100: http://acm.hdu.edu.cn/showproblem.php?pid=2100解法:高精度加法-26进制,A~Z分别代表0~26,求法和10进制一样code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>char a[300],b[300],c[300],d[300];int max(int x,int y){ if(x>y) return x; else return y;}int main(){ int x,y,s; 阅读全文
posted @ 2012-07-25 20:59 acmer-jun 阅读(300) 评论(0) 推荐(0) 编辑
摘要: hdu1856: http://acm.hdu.edu.cn/showproblem.php?pid=1856题意:输入n对朋友,求最多的朋友集合(直接或间接为朋友)解法:并查集:集合的合并及查询。code:#include<iostream>#include<cstdio>#include<cstdlib>const int maxn=10000002;int fa[maxn],ans[maxn];int find(int x) //路径压缩,复杂度为常数{ int fx=fa[x]; if(x!=fx)fa[x]=find(fx); return fa[ 阅读全文
posted @ 2012-07-25 20:54 acmer-jun 阅读(185) 评论(0) 推荐(0) 编辑
摘要: hdu1798: http://acm.hdu.edu.cn/showproblem.php?pid=1798题意:给出两个圆的圆心坐标和半径,求两圆相交面积code:#include<iostream>#include<cstdlib>#include<cstdio>#include<cmath>const double pi=acos(double(-1));double min(double x,double y){ if(x>y) return y; else return x;}int main(){ double x1,y1,r 阅读全文
posted @ 2012-07-25 20:50 acmer-jun 阅读(206) 评论(0) 推荐(0) 编辑
摘要: pku1018: http://poj.org/problem?id=1018题意:给出n种设备,每种设备有m家公司出售,对应宽带b[n][m]和价格p[n][m],现要求每种设备各选一个,要求所选设备bmin的最小值与所选设备总价格p的比值最大解法:贪心+枚举:将所有设备的b值从小到大枚举,再选出符合价格最小的设备。code:#include<iostream>#include<cstdio>#include<cstdlib>int b[150][150],p[150][150],m[150];const int inf=1<<29;int m 阅读全文
posted @ 2012-07-25 20:45 acmer-jun 阅读(178) 评论(0) 推荐(0) 编辑
摘要: pku1083: http://poj.org/problem?id=1083题意:有一条走廊,走廊两边为房间,一边的编号都为奇数(1、3……、399),一边的编号都为偶数(2、4……、400),现要从某个房间搬东西到另一房间,走廊很窄,不能共用,即若要用到同一段走廊,不能同时搬,搬一次需时10,求最小搬家时间解法:贪心:因为如果不冲突的话可以同时进行,所以最小次数为走廊需要用到的次数最多那段的次数,再乘以10即为最小时间。code:#include<iostream>#include<cstdio>#include<cstdlib>int ans[500] 阅读全文
posted @ 2012-07-25 20:35 acmer-jun 阅读(173) 评论(0) 推荐(0) 编辑
摘要: hdu1677: http://acm.hdu.edu.cn/showproblem.php?pid=1677题意:给出m个嵌套娃娃的数据(宽w、高h),求嵌套后最少娃娃数解法:贪心法+dp:类似最少拦截系统,这个问题其实是用dp求最长子序列的长度,先按宽从小到大排序,则小号可能可以嵌套在大号中,再依次判断后面的h是否比前面的大,若是,则取前面中h较大者(由贪心法可知)。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespac 阅读全文
posted @ 2012-07-25 20:29 acmer-jun 阅读(164) 评论(0) 推荐(0) 编辑
摘要: hdu3177: http://acm.hdu.edu.cn/showproblem.php?pid=3177题意:向一个体积为V的洞搬进东西,物品体积为v[i].a,需要的移动体积为v[i].b,问能否全部搬进洞中解法:贪心法:对于两件物品a1,b1;a2,b2,若先放1再放2,则V-a1>b2即V>a1+b2;若先放2再放1,则V-a2>b1即V>a2+b1,用贪心法可知要取a1+b2与a2+b1中较小者,即b-a大者先取code:#include<iostream>#include<cstdio>#include<cstdlib> 阅读全文
posted @ 2012-07-25 19:42 acmer-jun 阅读(112) 评论(0) 推荐(0) 编辑
摘要: hdu2037: http://acm.hdu.edu.cn/showproblem.php?pid=2037题意:求最多不相交区间:给出n个节目开始a[i]和结束b[i]的时间,求最多可看多少个节目方法:贪心法:将b[i]从小到大排序,每次取第一个区间,再剔除与所取区间相交的区间。如样例应取(1,3)、(3,4)、(5,10)、(10,15)、(15,19)code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std; 阅读全文
posted @ 2012-07-25 18:45 acmer-jun 阅读(138) 评论(0) 推荐(0) 编辑
摘要: hdu1257: http://acm.hdu.edu.cn/showproblem.php?pid=1257题意:导弹拦截系统:第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度,v[i]为依次发来的导弹高度,求最少需要多少套拦截系统解法:贪心法:如对于样例,首先启动第一套,拦截前三个导弹后,限定值为155,再启动第二套,限定值为300,对于后面的导弹,采用贪心法,与前面的限定值作比较,采用限定值最接近该高度且小于该限定值的系统,如果没有这种系统,则另启动新系统。code:#include<iostream>#include<cstdio>#inc 阅读全文
posted @ 2012-07-25 17:53 acmer-jun 阅读(187) 评论(0) 推荐(0) 编辑

导航