2011年7月22日

poj 3298 Antimonotonicity

摘要: #include<iostream> //DP, 与sicily 1685. Missile 类似,但数据规模较大,O(n^2)肯定会TLE#include<stdio.h>using namespace std;int num[30010],ans[30010],odd,even; //ans[i]存放形成的序列上的第i个数,假若i表示偶数,则ans[i]<ans[i+1],所以第偶数上的值越小越好,而第奇数上的值越大越好int main(){ int t,n; cin>>t; while(t--) { scanf("%d",&a 阅读全文

posted @ 2011-07-22 23:12 sysu_mjc 阅读(157) 评论(0) 推荐(0)

poj 3970 Party

摘要: #include<iostream> //求任意多边形面积#include<cmath>using namespace std;struct point{ double x,y;}p[1000];int main(){ int n; while(cin>>n,n) { for(int i=0;i<n;++i) cin>>p[i].x>>p[i].y; if(n<3) { printf("0\n"); continue; } double area=0; for(int i=0;i<n;++i) a 阅读全文

posted @ 2011-07-22 23:10 sysu_mjc 阅读(181) 评论(0) 推荐(0)

poj 3660 Cow Contest

摘要: // 题意: 有N只牛,M场比赛的结果,(a,b)代表奶牛a能战胜奶牛b,问有多少只奶牛的排名可以确定// 思路: floyd算法的变形,给出一些点之间的大小关系,问有多少点的次序是固定的#include <iostream> // floyd算法using namespace std; #define maxn 102int distF[maxn][maxn];// distF[a][b]= 1 表示a>b, 2 表示a<b ,0 表示 a,b的大小不确定int n,m,i,j;void floyd() ... 阅读全文

posted @ 2011-07-22 23:08 sysu_mjc 阅读(130) 评论(0) 推荐(0)

poj 3272 Cow Traffic

摘要: // 题意: 求出由入度为0的源点到汇点的所有路径中使用最频繁那条边总共使用的次数// 思路: 设f[i] 为入度为0的源点到节点i的路径条数, g[i] 为节点i到汇点N的路径条数// 枚举每条边(s为始点,t为终点),所有边的f[s]*g[t]的最大值即为答案#include<iostream> //拓扑排序#include<deque>using namespace std;#define MAXN 5002int n,m,table[MAXN][MAXN],side[50002][2];int f[MAXN],g[MAXN],in[MAXN],out[MAXN] 阅读全文

posted @ 2011-07-22 23:07 sysu_mjc 阅读(259) 评论(0) 推荐(0)

poj 3260 The Fewest Coins

摘要: /*题意:John带了n种币值Vi的确定数量Ci的硬币,而shopkeeper的硬币无限多.给出T,求John支付的硬币数目加上售货员找零的硬币数目的最小值。如果无法支付T,输出-1 支付时硬币数量有限制,为多重背包问题. 找零时硬币数量无限制,为完全背包问题*/#include<iostream> //多重背包和完全背包using namespace std; int main() { int n,t,euro[110],num[110],dp[30000],maxn; cin>>n>>t; int mx=0; for(int i=1;i<=n;++ 阅读全文

posted @ 2011-07-22 23:06 sysu_mjc 阅读(228) 评论(0) 推荐(0)

poj 3080 Blue Jeans

摘要: #include <iostream> //KMP+枚举#include<string>using namespace std;#define len 60char str[10][100],base[100]; int next[100]; void get_next(char B[],int t) //B[1]--B[t]{ next[1]=0; int j=0; for(int i=2;i<=t;++i) { while(j>0&&B[j+1]!=B[i]) j=next[j]; if(B[j+1]==B[i]) j=j+1; next 阅读全文

posted @ 2011-07-22 23:03 sysu_mjc 阅读(151) 评论(0) 推荐(0)

poj 3041 Asteroids

摘要: // 题意:N*N矩阵,有一些格子里有小行星,一颗炮弹能够消灭掉矩阵中一行或一列的全部小行星,// 问消灭掉所有小行星所需的最小炮弹数目。// 思路:令集合X和集合Y分别表示矩阵的行和列,将矩阵每一行看成集合X的点,每一列看成集合Y的点,// 如果第i行第j列有小行星则将Xi和Yj连一条边,这样就构成一个二分图,// 题目就是求二分图的最小覆盖点数(即最少的行和列),转化成二分图的最大匹配数#include<iostream> //求二分图的最小覆盖点数#include<cstring>using namespace std;int n;int edge[510][51 阅读全文

posted @ 2011-07-22 23:02 sysu_mjc 阅读(130) 评论(0) 推荐(0)

poj 3013 Big Christmas Tree

摘要: View Code #include<iostream> #include<deque>using namespace std;#define maxn 50002const __int64 inf=(__int64)1<<63-1; struct Edge { int v; int weight; int next;}Vertex[4*maxn]; int head[maxn],curr;int cases,v,e,i,j,edge[maxn][3],w[maxn];void add_edge(int s,int t,int w) //新增结点不用申请空间 阅读全文

posted @ 2011-07-22 23:01 sysu_mjc 阅读(187) 评论(0) 推荐(0)

poj 2704 Pascal's Travels

摘要: /*题意:在一个N×N的底盘上,每一格有一个非负整数,表示在那一格可以向右或向下走几步。问每次只能向右或向下走,从左上角的格子走到右下方的格子有多少种不同的方案。dp[i][j]表示到从左上角到(i,j)的方案数dp[i][j]+=(dp[i][u])+(dp[v][j]),1<=u<j, 1<=v<i,如果(i,u)能走到(i,j), (v,j)能走到(i,j)*/#include<iostream> //DP#include<cstring>using namespace std;char table[40][40];long lo 阅读全文

posted @ 2011-07-22 22:59 sysu_mjc 阅读(135) 评论(0) 推荐(0)

poj 2882 Food Cubes

摘要: //sicily3 1114. Food Cubes//一立方体,给出某些填充的三维点,求有多少孔穴 //要注意孔的定义:A hole is a continuous empty space surrounded by food cubes in all six directions.//如果某区域与边界相临,那么它不能算做是孔.宽搜遍历时会忽略掉这种特殊情况,得到的孔数目比答案要多//方法是扩大边界, 题意:all between 1 and 100 inclusive,所以可以扩大到 0-101。最后得到的孔的数目减去 1 即为答案//为什么扩大边界后只要减去 1 就可以了,分两种情况讨论 阅读全文

posted @ 2011-07-22 22:59 sysu_mjc 阅读(174) 评论(0) 推荐(0)

poj 2689 Prime Distance

摘要: //先建立素数表,再用这些素数去筛掉[L,U]区间内的合数#include <iostream> //线性筛素数#include<cmath>using namespace std; #define maxn 46341 //floor(sqrt(double(INT_MAX))+0.5)=46341bool isPrime[maxn+1]; int prime[10000],cnt; void makePrime() //快速线性筛法建立素数表 { memset(isPrime,true,sizeof(isPrime)); cn... 阅读全文

posted @ 2011-07-22 22:58 sysu_mjc 阅读(149) 评论(0) 推荐(0)

poj 2663 Tri Tiling

摘要: #include<iostream> //DP, 与sicily4 1121. Tri Tiling 完全相同#include<stdio.h>using namespace std;int dp[31]={1}; //0的时候就是不放置,是1...int main(){ for(int i=2;i<=30;i+=2) { dp[i]=3*dp[i-2]; //在3x2矩阵中可以有3种放置方法,表示[i-2,i]不与前面相连接,单独拼凑成 for(int j=0;j<=i-4;j+=2) dp[i]+=2*dp[j]; //当j=i-4,i-6...时,表示 阅读全文

posted @ 2011-07-22 22:57 sysu_mjc 阅读(137) 评论(0) 推荐(0)

poj 2632 Crashing Robots

摘要: #include<iostream> //简单模拟题,千万要注意所给表格的x,y轴表示法与一般的相反using namespace std;char ch,action;int pos[100000][2],dir[100000],vis[102][102];int k,a,b,x,y,n,m,d,robot,repeat;int main(){ cin>>k; while(k--) { cin>>a>>b>>n>>m; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;++i) 阅读全文

posted @ 2011-07-22 22:56 sysu_mjc 阅读(142) 评论(0) 推荐(0)

poj 2491 Scavenger Hunt

摘要: #include<iostream>#include<string>#include<map>using namespace std;string str[500][2];int main(){ int cases,v,i; cin>>cases; for(int id=1;id<=cases;++id) { scanf("%d",&v); map<string,string> col; for(i=1;i<v;++i) { cin>>str[i][0]>>str[i][ 阅读全文

posted @ 2011-07-22 22:54 sysu_mjc 阅读(190) 评论(0) 推荐(0)

poj 2484 A Funny Game

摘要: /*当n==1 || n==2时,明显先手必胜。当n==3时,明显先手必败。由于每次只可取1或2个,而取2个时,2个必须相邻,推断有:当n>3时,若n为偶数,先手无论如何取,后手可在先手对称的位置上取同等数量,于是先手必败。若n为奇数,先手取1个时,后手可在先手对称的位置上取2个,之后无论先手如何取,后手都可在先手对称的位置上取同等数量,先手必败。如果先手一开始取2个时,后手可在先手对称的位置上取1个,之后还剩下偶数个,可如上推出先手必败。故当 n>3时,先手必败*/#include<iostream>#include<stdio.h>using names 阅读全文

posted @ 2011-07-22 22:53 sysu_mjc 阅读(198) 评论(2) 推荐(0)

poj 2367 Genealogical tree

摘要: #include<iostream>//简单拓扑排序usingnamespace std;int n,side[102][102],in[102],path[102];int main(){ cin>>n; int a,rear=0; for(int id=1;id<=n;++id) { while(cin>>a&&a) { side[id][a]=1; in[a]++; } } for(int i=1;i<=n;++i) if(in[i]==0) ... 阅读全文

posted @ 2011-07-22 22:52 sysu_mjc 阅读(159) 评论(0) 推荐(0)

poj 2305 Basic remains

摘要: //在b进制下,求p%m,其中m, contains up to 9 digits,所以m是在int范围内//把p和m转化为十进制数,而p边转化边模m,这样余数是以十进制来表示,再转化成b进制输出即可#include <iostream>#include <string>using namespace std;int main(){ char s1[1002],s2[10]; int m,b; while(scanf("%d",&b)&&b) { scanf("%s%s",s1,s2); m=0; int 阅读全文

posted @ 2011-07-22 22:51 sysu_mjc 阅读(183) 评论(0) 推荐(0)

poj 2239 Selecting Courses

摘要: // 题意:一共有n门课程,每门课都有对应的几个可以选择的上课时间,为星期几的第几节。// 一个时间段只能上一门课,问最多可以选择几门课#include<iostream> //二分图的最大匹配#include<cstring>using namespace std;int edge[400][100],vis[100],link[100]; int find(int a){ for(int i=1;i<=84;++i) // V1子集下标范围[1,n],V2子集下标范围[1,84] { if( edge[a][i] ... 阅读全文

posted @ 2011-07-22 22:50 sysu_mjc 阅读(199) 评论(0) 推荐(0)

poj 2191 Mersenne Composite Numbers

摘要: #include<iostream>using namespace std;bool is_prime(int a){ if(a==1) return false; for(int i=2;i*i<=a;++i) if(a%i==0) return false; return true;}long long num[100]={1},ans[10];int main(){ int n; cin>>n; for(int i=1;i<64;++i) num[i]=num[i-1]*2... 阅读全文

posted @ 2011-07-22 22:49 sysu_mjc 阅读(194) 评论(0) 推荐(0)

poj 2226 Muddy Fields

摘要: /* 题意: 有R×C方阵,'*'表示洼地,需要铺上宽度为1的木板,长度不限, 木板只能横放或竖放.木板可以重叠,但不能覆盖'.'(草地) 求覆盖所有'*'的最少木板数. 思路:将所有横向且连续(一个或以上)的'*'看成一个点并对其进行编号,所有的编号都为集合X内的编号。 同样地,将所有的竖向且连续(一个或以上)的'*'看成一个点并对其编号,所有的编号都为集合Y内的编号 对于原图里的'*',设其在横向X编号为i,在竖向Y编号为j,则在点 Xi 和点 Yj 间连一条边, 这样构成一个二分图, 阅读全文

posted @ 2011-07-22 22:49 sysu_mjc 阅读(180) 评论(0) 推荐(0)

poj 2034 Anti-prime Sequences

摘要: #include<iostream>using namespace std; bool isPrime[10002]; int prime[1000],total; //线性筛法寻找素数 void makePrime() { total=0; memset(isPrime,true,sizeof(isPrime)); for(int i=2;i<=maxn;i++) { if(isPrime[i]) prime[++total]=i; for(int j=1;j<=total&& i*prime[j]<=maxn; j++) { isPrime[i 阅读全文

posted @ 2011-07-22 22:48 sysu_mjc 阅读(288) 评论(0) 推荐(0)

poj 1730 Perfect Pth Powers

摘要: #include<iostream>#include<math.h>using namespace std;#define eps 1e-2 //其他精度数也同样可以int main(){ int n; while(cin>>n&&n) { //n有负数的可能,比如 -1073741824,正解应该是 15, (-4)^15=-1073741824 int t=n>0?1:-1; for(int i=31;i>=1;--i) { double x=ceil(pow(double(n)*t,1.0/i))*t; //向上取整 dou 阅读全文

posted @ 2011-07-22 22:47 sysu_mjc 阅读(134) 评论(0) 推荐(0)

poj 2033 Alphacode

摘要: #include<iostream>#include<string>using namespace std;int arr[10000],len,ans[10000];int dp(int id){ if(ans[id]!=-1) return ans[id]; int s; if(id==len) s=1; else if(id==len-1) s=arr[id]>0?1:0; else if(arr[id]==0) s=0; else if(10*arr[id]+arr[id+1]<=26) s=dp(id+1)+dp(id+2); else s=dp( 阅读全文

posted @ 2011-07-22 22:47 sysu_mjc 阅读(194) 评论(0) 推荐(0)

poj 1634 Who's the boss?

摘要: #include<iostream> //排序#include<stdio.h>#include<algorithm>#include<vector>using namespace std;int n,m,q,y,k;struct node{ int id,sala,height; int boss,child; //sub存储第一层下属的位置,child记录所有下属subordinate的数目 vector<int> sub; bool operator<(const node& o)const //降序 { retu 阅读全文

posted @ 2011-07-22 22:46 sysu_mjc 阅读(235) 评论(0) 推荐(0)

poj 1654 Area

摘要: #include<iostream> //多边形面积#include<string>using namespace std;struct point{ int x,y;}p[1000010];int move[10][2]={{},{-1,-1},{0,-1},{1,-1},{-1,0},{},{1,0},{-1,1},{0,1},{1,1}};int main(){ int cases; string str; cin>>cases; while(cases--) { cin>>str; int n=str.size()-1; if(n< 阅读全文

posted @ 2011-07-22 22:46 sysu_mjc 阅读(136) 评论(0) 推荐(0)

poj 1620 Phone Home

摘要: #include<iostream> //求图的色数,即使各相邻顶点的颜色不相同所需的最小色数.数据量小,直接枚举+DFS#include<stdio.h>#include<cmath>#include<cstring>using namespace std;struct node{ double x,y;}point[20];int n,cnt[20][20],color[20],num,suc;void dfs(int i){ for(int c=1;c<=num;++c) { int flag=1; for(int j=0;j< 阅读全文

posted @ 2011-07-22 22:41 sysu_mjc 阅读(202) 评论(0) 推荐(0)

poj 1580 String Matching

摘要: #include<iostream>#include<string>using namespace std;int gcd(int n,int m) //辗转相除法{ int r; if(n<m)swap(n,m); while(m!=0) { r=n%m; n=m; m=r; //永远符合n>=m } return n;}int main(){ char str[10000],ctr[10000]; while(cin>>str&&strcmp(str,"-1")!=0) { cin>>ctr; 阅读全文

posted @ 2011-07-22 22:40 sysu_mjc 阅读(175) 评论(0) 推荐(0)

poj 1511 Invitation Cards

摘要: #include<iostream> //Dijkstra+优先队列 1735MS!#include<queue>#define maxn 1000002using namespace std;struct Edge { int v; int weight; int next;}Vertex[maxn];int head[maxn],curr; const __int64 inf=(__int64)1<<63-1; int cases,p,e,i,j,edge[maxn][3];void graph(int tag){ int u,v,w; curr=0; 阅读全文

posted @ 2011-07-22 22:34 sysu_mjc 阅读(139) 评论(0) 推荐(0)

poj 1493 Machined Surfaces

摘要: #include<iostream> #include<string>#include<algorithm>using namespace std;int main(){ string str[20]; int blank[20],n,low,sum; while(cin>>n&&n) { scanf("\n"); for(int i=0;i<n;++i) { getline(cin,str[i]); blank[i]=0; for(int j=0;j<str[i].size();++j) if(s 阅读全文

posted @ 2011-07-22 22:33 sysu_mjc 阅读(133) 评论(0) 推荐(0)

poj 1426 Find The Multiple

摘要: // 题意: 求能整除n的十进制数,由0或1组成#include<iostream> // DFSusing namespace std;int n,ok;int num[100]={1}; //一定是以 1 开头int mod(int cur) //判断num[0]-num[cur]所表示的值能否被n整除{ int s=0; for(int i=0;i<=cur;++i) { s=(s*10+num[i])%n; } return s;}void dfs(int cur){ if(cur>=100) /... 阅读全文

posted @ 2011-07-22 22:32 sysu_mjc 阅读(133) 评论(0) 推荐(0)

poj 1325 Machine Schedule

摘要: /* 题意: 有两台机器A和B,分别有n种和m种不同的模式, 有k个工作,每个工作都可以在那两个机器的某种特定的模式下处理, 如job[0]既可以在A机器的3号模式下处理,也可以在B机器的4号模式下处理, 初始时两台机器都在0号模式下工作,机器的工作模式改变只能通过重启来改变, 通过改变工作的顺序和分配每个工作给合适的机器可以减少重启机器的次数,求重启机器的最少次数 思路: 令X,Y分别是机器A和B的工作模式集合,工作在A和B机器上的工作模式分别为X[a]和Y[b],让X[a]与Y[b]相连, 这样我们就可以X和Y作为两个顶点集合来建立二分图,重启机器的次数就是覆盖所有的边的顶点数。 所以问. 阅读全文

posted @ 2011-07-22 22:31 sysu_mjc 阅读(169) 评论(0) 推荐(0)

poj 1365 Prime Land

摘要: #include<iostream>#include<string>using namespace std;bool isprime(int n){ for(int i=2;i*i<=n;++i) if(n%i==0) return false; return true;}int pe[1000];int main(){ string str; while(getline(cin,str)&&str!="0") { int s=0,id=0; for(int i=0;i<str.size();++i) { if(str[i 阅读全文

posted @ 2011-07-22 22:31 sysu_mjc 阅读(165) 评论(0) 推荐(0)

poj 1281 MANAGER

摘要: #include<iostream> //模拟题#include<set>using namespace std;int out[100000],query[100000];bool first;int main(){ int max_cost,len; while(cin>>max_cost) { cin>>len; for(int i=0;i<len;++i) cin>>query[i]; char ch; int p=1,x,r=1; set<int> col; while(cin>>ch,ch!= 阅读全文

posted @ 2011-07-22 22:30 sysu_mjc 阅读(260) 评论(0) 推荐(0)

poj 1270 Following Orders

摘要: // 给出一个字母表和一些字母对(c1,c2)表示c1<c2// 求出所有满足要求的排列,并按照字典序输出#include<iostream> //拓扑排序 + dfs#include<algorithm>#include<string>using namespace std;int dict[30],edge[30][30],vis[30],rear,path[30],in[30];void topo_sort(int pos){ if(pos==rear) { for(int i=0;i<rear;++i) cout<<char( 阅读全文

posted @ 2011-07-22 22:29 sysu_mjc 阅读(184) 评论(0) 推荐(0)

poj 1254 Hansel and Grethel

摘要: #include<iostream> //求两直线交点,交点是存在的#include<stdio.h>#include<cmath>using namespace std;#define pi acos(-1.0)int main(){ int cases; double x1,y1,d1,x2,y2,d2,k1,k2,x,y; cin>>cases; while(cases--) { cin>>x1>>y1>>d1>>x2>>y2>>d2; k1=tan((450-d1)* 阅读全文

posted @ 2011-07-22 22:28 sysu_mjc 阅读(183) 评论(0) 推荐(0)

poj 1256 Anagram

摘要: #include<iostream>#include<string>#include<algorithm>using namespace std;int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.{ if(tolower(a)!=tolower(b)) return tolower(a)<tolower(b); else return a<b;}int main 阅读全文

posted @ 2011-07-22 22:28 sysu_mjc 阅读(119) 评论(0) 推荐(0)

poj 1252 Euro Efficiency

摘要: // 给出6枚不同面值(在1到100之间)的硬币,通过 加减(注意可以减)凑出1到100的钱数,我们关心的是最少要用到几枚硬币,// 最后求出平均值,并找出其中最大值#include<iostream> // BFS最短路搜索#include<stdio.h>#include<algorithm>#include<numeric>using namespace std;int euro[20],q[200],cnt[200],vis[200];int main(){ int cases,i; scanf("%d",&c 阅读全文

posted @ 2011-07-22 22:26 sysu_mjc 阅读(206) 评论(0) 推荐(0)

poj 1129 Channel Allocation

摘要: //参考 poj9 1620 Phone Home#include<iostream> //求图的色数,即使各相邻顶点的颜色不相同所需的最小色数.数据量小,直接枚举+DFS#include<stdio.h>#include<cstring>using namespace std;int n,cnt[30][30],color[30],num,suc;void dfs(int i){ for(int c=1;c<=num;++c) { int flag=1; for(int j=0;j<n;++j) if(cnt[i][j]==1&& 阅读全文

posted @ 2011-07-22 22:20 sysu_mjc 阅读(189) 评论(0) 推荐(0)

poj 1094 Sorting It All Out

摘要: // 题意:给出n个字母和一系列不等式,判断是否能确定所有字母顺序// 输出在最早在第几个不等式处可以确定下唯一的顺序或判断出矛盾,或者到最后也无法确定唯一的顺序#include<iostream> //拓扑排序using namespace std;int table[26][26],path[26],in[26],origin_in[26]; //in表示入度数int main(){ char ch[4]; int n,m,a,b; while(cin>>n>>m&&n) { memset(table,0,sizeo... 阅读全文

posted @ 2011-07-22 22:17 sysu_mjc 阅读(119) 评论(0) 推荐(0)

poj 3753 根据关键字进行字符串拷贝

摘要: #include<iostream>#include<string>using namespace std;int main(){ string str1,str2; while(cin>>str1) { while(cin>>str2&&str2!="END") { if(str2=="NULL") { printf("0 NULL\n");continue; } int id=str1.find(str2); if(id==0) { printf("0 NU 阅读全文

posted @ 2011-07-22 20:43 sysu_mjc 阅读(160) 评论(0) 推荐(0)

poj 3349 Snowflake Snow Snowflakes

摘要: #include<iostream> //哈希函数,拉链法#include<vector>using namespace std;const int mv=99991;int data[100002][6];vector<int> hash[100002];bool same(int a[6],int b[6]) //a[]的下标j总是从0到6,固定a,而b[]的起始下标i从0到6,绕顺时针(向右)和逆时针旋转{ int tag; for(int i=0;i<6;++i) { tag=0; for(int j=0;j<6;++j) { if(a[ 阅读全文

posted @ 2011-07-22 20:42 sysu_mjc 阅读(121) 评论(0) 推荐(0)

poj 3667 Hotel

摘要: // 题意: 旅店登记,有n个房间,有两种操作: 1.找一段最靠前的连续d个空房间; 2.退订[x,x-d+1]段的房间#include<iostream> //线段树using namespace std;struct node{ int l,r,blank; int lb,rb; //lb(rb)记录从左(右)端点开始的连续空的线段长}tree[200010];void build_tree(int n,int s,int t){ tree[n].l=s;tree[n].r=t; tree[n].blank=tree[n].lb=... 阅读全文

posted @ 2011-07-22 20:42 sysu_mjc 阅读(195) 评论(0) 推荐(0)

poj 3259 Wormholes

摘要: /* 题意:John的农场里n块地,M条路连接两块地,是双向边, W个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退w秒。 问能否在从某块地出发后又回来,看到了离开之前的自己,即求负权回路 思路:Bellman-Ford算法,源点不明确,求负权回路 当源点不明确时: 与SPFA算法不同,当源点不明确时,Bellman-Ford算法不用添加超源点, 因为它对图中每一条边都会做n-1次松弛,就保证每个顶点都得到更新。 对于此题,如何控制初值,一个比较原始的想法是,枚举起点,这样的时间复杂度大约为O(n^4),但是还是可以过... 而经过观察发现,假设有一个原点0,到所有点的距离都. 阅读全文

posted @ 2011-07-22 20:41 sysu_mjc 阅读(169) 评论(0) 推荐(0)

poj 3159 Candies

摘要: /* 题意: 在满足b1-a1<=c1,b2-a1<=c2...的情况下,flymouse( N )希望自己分得的糖果数尽量多于snoopy( 1 )b-a<=c 不等式正好可以构成最短路的三角不等式:dv <= du + w(u,v)比如顶点A,B,C有如下关系:db-da<=2,dc-da<=5,dc-db<=2,构成一三角形ABC,AB(A指向B),AC(A指向C),BC(B指向C),由dc-db<=2,db-da<=2,知dc<=db+2,db<=da+2,所以dc<=da+4而原条件 dc-da<=5,即d 阅读全文

posted @ 2011-07-22 20:40 sysu_mjc 阅读(155) 评论(0) 推荐(0)

poj 3095 Linear Pachinko

摘要: #include<iostream> //水题#include<string>using namespace std;int main(){ string str; int i,j; while(cin>>str&&str!="#") { int s=0; for(i=0;i<str.size();++i) { if(str[i]=='.') s+=100; else if(str[i]=='|') { for(j=i-1;j>=0&&str[j]=='_ 阅读全文

posted @ 2011-07-22 20:39 sysu_mjc 阅读(157) 评论(0) 推荐(0)

poj 3090 Visible Lattice Points

摘要: #include<iostream> //先打表,不然TLE using namespace std;int gcd(int n,int m) { int r; if(n<m)swap(n,m); while(m!=0) { r=n%m; n=m; m=r; } return n;}int g[1002][1002],res[1002];int main(){ for(int i=1;i<=1000;++i) for(int j=1;j<=1000;++j) if(gcd(i,j)==1) g[i][j]=1; for(int k=1;k<=1000;++k 阅读全文

posted @ 2011-07-22 20:38 sysu_mjc 阅读(161) 评论(0) 推荐(0)

poj 2480 Longge's problem

摘要: /*设F(N)=∑gcd(i, N) ,1<=i<=N性质:F[P^k]=k×(P^k-P^(k-1))+P^k ,P为质数 (详细的证明放在后面)且F(N)是积性函数,即当GCD(M,N)=1时,有 F[M×N]=F[M]×F[N],又因为一个正整数总可以表示成素数(它的质因子)的乘积: N = p1^k1 * p2^k2 * ... * pn^kn (这里p1,p2,..pn是素数,当然相互之间是互质的)所以F(N)=F[p1^k1] * F[p2^k2] * ... * F[pn^kn] = (k1×(p1^k1-p1^(k1-1))+ 阅读全文

posted @ 2011-07-22 20:33 sysu_mjc 阅读(143) 评论(0) 推荐(0)

poj 2485 Highways

摘要: // 题意:找出最小生成树的最大边#include<iostream> //最小生成树Prim算法using namespace std;struct MST //最小生成树的边{ int st,ed,w;}mst[1000];int n,edge[1000][1000];int ans; //ans保存最小生成树的最长边void Prim(){ int i,j,k; for(i=0;i<n-1;i++) //默认选择节点0加入生成树 { mst[i].st=0;mst[i].ed=i+1; mst[i... 阅读全文

posted @ 2011-07-22 20:33 sysu_mjc 阅读(121) 评论(0) 推荐(0)

poj 2436 Disease Management

摘要: //有 D(<=15) 种疾病在 N 头牛中,告知每头牛所患的病,问至多能选择多少头牛,使得它们所患的病总数不超过 K//方法:枚举所有疾病种类(不大于 k),检查每头牛患的病是否落在该范围内,比较找出最大的数目#include<iostream> //位运算using namespace std;int cow[1002]; //cow[]表示牛携带的疾病的值,若患有 i 种疾病,则第(i-1)位上的值是1 .假如患有第 1 和 5 种疾病,则表示为 10001=17int main(){ int n,d,k; scanf("%d%d%d",&n 阅读全文

posted @ 2011-07-22 20:32 sysu_mjc 阅读(173) 评论(0) 推荐(0)

poj 2478 Farey Sequence

摘要: #include <iostream> //欧拉函数, 给定n,求φ(1)到φ(n)的和using namespace std;const int max_n=1000000;long long phi[max_n+2],sum[max_n+2]; //这里数组空间起码要稍微大于max_nvoid phi_table() //欧拉函数表{ for(int i=2;i<=max_n;++i) phi[i]=0; phi[1]=1; for(int i=2;i<=max_n;++i) if(!phi[i]) ... 阅读全文

posted @ 2011-07-22 20:32 sysu_mjc 阅读(222) 评论(2) 推荐(0)

poj 2387 Til the Cows Come Home

摘要: #include <iostream> //单源顶点最短路径:迪杰斯特拉(Dijkstra)算法using namespace std; const int MaxWeight=1000002; //最大值应该是1000*100+1 int Vertex,edge[1002][1002],distD[1002],i,j;void init(){ for(i=1;i<=Vertex;i++) //结点坐标都是从1开始的 for(j=1;j<=Vertex;j++) { if(i==j) edge[i][j]=0; else edge[i][j]=MaxWeight; }} 阅读全文

posted @ 2011-07-22 20:31 sysu_mjc 阅读(165) 评论(0) 推荐(0)

poj 2421 Constructing Roads

摘要: #include<iostream> //最小生成树primusing namespace std;const int max_ve=105,MaxWeight=2000;int n,edge[max_ve][max_ve],sum; //n,m分别记录顶点数和边数struct MinTree //最小生成树的边{ int begin,end,weight;}mintree[max_ve];void Prim(){ int min,v,com,i,j,k; for(i=0;i<n-1;i++) //对mintree进行初始化,顶点下标从0开始 { mintree[i].beg 阅读全文

posted @ 2011-07-22 20:31 sysu_mjc 阅读(213) 评论(0) 推荐(0)

poj 2240 Arbitrage

摘要: // 题意: 套汇问题,给出n种货币,和m种兑换利率,问是否存在一种盈利的兑换方式// 即找到一个圈,使沿着这个圈兑换钱币后钱数增加// 思路: 路径为汇率,由于自环汇率是1,因此,我们要找一个圈作用的结果大于1。// floyd算法的变形,注意路径松弛的时候要改成相乘,另外,用map进行字符串与节点编号的映射#include <iostream> // floyd算法#include<string>#include<map>using namespace std;double distF[32][32];int n,m,i,j;void floyd(){ 阅读全文

posted @ 2011-07-22 20:30 sysu_mjc 阅读(171) 评论(0) 推荐(0)

poj 1860 Currency Exchange

摘要: #include <iostream> //Bellman-Ford算法的变形,参照 poj 2240 Arbitrageusing namespace std;const int maxnum = 102;int n,m,s,a,b;double v,r1,c1,r2,c2;typedef struct Edge{ int u, v; double ex,com; }Edge;Edge edge[1000]; double dist[maxnum]; int nodenum, edgenum; void input(){ cin>>n>>m>> 阅读全文

posted @ 2011-07-22 20:28 sysu_mjc 阅读(204) 评论(0) 推荐(0)

poj 1845 Sumdiv

摘要: #include<iostream> //求 A^B 的所有约数之和using namespace std;const __int64 mod=9901;__int64 fac[1000],num[1000],rear;__int64 root(__int64 a,__int64 b) //二分法求解 a^b { if(b==0) return 1; else if(b==1) return a%mod; __int64 c=root(a,b/2),d=c*c%mod; if(b%2==0) return d; else return d*a%mod;}void factor(__ 阅读全文

posted @ 2011-07-22 20:27 sysu_mjc 阅读(195) 评论(0) 推荐(0)

poj 1753 Flip Game

摘要: //题意:在一个4*4的棋盘上翻转棋子,棋子只有黑白两色,翻转了一个棋子以后,他的上下左右四个方向的棋子都得跟着变色,//最后使得棋盘上所有的棋子都是白色,或者都是黑色。统计最少需要翻转棋子的个数。#include<iostream> //bfs+位运算 参照 poj8 1166 The Clocks#include <stdio.h>using namespace std;const int mod=0x55555555; //mod=(0101 0101 0101 0101 0101 0101 0101 0101)十六进制,实际上是16,4*4个 01 int f. 阅读全文

posted @ 2011-07-22 20:26 sysu_mjc 阅读(176) 评论(0) 推荐(0)

poj 1664 放苹果

摘要: #include<iostream> //构造非下降序列(这样才能不重复),统计其个数using namespace std;int t,m,n,ans[15],sum,s;void dfs(int r){ if(r==n) { if(m-sum>=ans[r-1]) s++; return ; } for(int i=ans[r-1];sum+i<=m;++i) { ans[r]=i; sum+=ans[r]; dfs(r+1); sum-=ans[r]; }}int main(){ int t; cin>>t; while(cin>>m> 阅读全文

posted @ 2011-07-22 20:25 sysu_mjc 阅读(98) 评论(0) 推荐(0)

poj 1502 MPI Maelstrom

摘要: #include <iostream> //最短路径的最长边using namespace std; const int MAXN=120;const int INF=1<<30; int n,edge[MAXN][MAXN],distD[MAXN],vis[MAXN];int ans;void Dijstra(int st) //从源点st到其余各顶点的最短路径长度{ memset(vis,0,sizeof(vis)); for(int i=0;i<n;++i) //结点... 阅读全文

posted @ 2011-07-22 20:24 sysu_mjc 阅读(173) 评论(0) 推荐(0)

poj 1656 Counting Black

摘要: #include<iostream>#include<string>using namespace std;int table[101][101];int main(){ string str; int n,x,y,l; cin>>n; while(n--) { cin>>str>>x>>y>>l; if(str=="BLACK") for(int i=x;i<x+l;++i) for(int j=y;j<y+l;++j) table[i][j]=1; else if(str== 阅读全文

posted @ 2011-07-22 20:24 sysu_mjc 阅读(111) 评论(0) 推荐(0)

poj 1459 Power Network

摘要: #include<iostream> //多源多汇网络流,参考poj8 1273 Drainage Ditches#include<queue>using namespace std;#define inf 0x7fffffffint flow[102][102],cap[102][102],a[102],p[102],s,t,f;int n,np,nc,m,u,v,z;void ek(){ queue<int> col; memset(flow,0,sizeof(flow)); f=0; while(1) { memset(a,0,sizeof(a)); 阅读全文

posted @ 2011-07-22 20:23 sysu_mjc 阅读(122) 评论(0) 推荐(0)

poj 1273 Drainage Ditches

摘要: #include<iostream> //网络流#include<queue>using namespace std;#define inf 0x7fffffffint flow[202][202],cap[202][202],a[202],p[202],s,t,f;int main(){ int n,m; while(cin>>n>>m) { memset(cap,0,sizeof(cap)); int si,ei,ci; while(n--) { cin>>si>>ei>>ci; cap[si][ei]+= 阅读全文

posted @ 2011-07-22 20:22 sysu_mjc 阅读(125) 评论(0) 推荐(0)

poj 3984 迷宫问题

摘要: #include<iostream>#include<deque>using namespace std;struct node{ int path[25][2]; int t;}ans[30];int arr[5][5],vis[5][5],pos[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int main(){ for(int i=0;i<5;++i) for(int j=0;j<5;++j) cin>>arr[i][j]; deque<node> col; int m=1; ans[0].path[0] 阅读全文

posted @ 2011-07-22 20:20 sysu_mjc 阅读(124) 评论(0) 推荐(0)

poj 3620 Avoid The Lakes

摘要: #include<iostream> //dfsusing namespace std;bool visited[105][105],rec[105][105];int n,m,k,s,ans[8]={-1,0,1,0,0,-1,0,1};void dfs(int r,int c){ s++;visited[r][c]=1; for(int i=0;i<4;++i) { int a=ans[2*i],b=ans[2*i+1]; if(r+a>=1&&r+a<=n&&c+b>=1&&c+b<=m & 阅读全文

posted @ 2011-07-22 20:19 sysu_mjc 阅读(106) 评论(0) 推荐(0)

poj 3641 Pseudoprime numbers

摘要: #include <iostream> //二分using namespace std;__int64 p;bool prime(__int64 n){ for(__int64 i=2;i*i<=n;++i) if(n%i==0) return false; return true;}__int64 query(__int64 a,__int64 n){ if(n==1) return a; __int64 q=query(a,n/2),t=(q*q)%p; if(n%2==0) return t; else return (t*a)%p;}int main(){ __int 阅读全文

posted @ 2011-07-22 20:19 sysu_mjc 阅读(158) 评论(0) 推荐(0)

poj 3616 Milking Time

摘要: #include<iostream> //dp#include<algorithm>using namespace std;int dp[1005],n,m,r;struct node{ int s,e,eff; bool operator<(const node& o) { return s<o.s||(s==o.s&&e<o.e); }}ans[1005];int main(){ int i,j; cin>>n>>m>>r; for(i=0;i<m;++i) { cin>> 阅读全文

posted @ 2011-07-22 20:18 sysu_mjc 阅读(118) 评论(0) 推荐(0)

poj 3280 Cheapest Palindrome

摘要: //参照poj1 1159 Palindrome// 题意:对一字符串增加或删除字符来使其变成回文字符串,而增加或删除字符都有一个花费,// 求解使该字符串变成回文串需要的最小花费.#include <iostream> // DP + 滚动数组#include <string>using namespace std;#define MAXN 2005int ans[2][MAXN];int main(){ int n,m; char s[MAXN]; scanf("%d%d%s",&m,&n,s); char ch[2]; int 阅读全文

posted @ 2011-07-22 20:17 sysu_mjc 阅读(151) 评论(0) 推荐(0)

poj 3461 Oulipo

摘要: #include <iostream> //KMP算法using namespace std;char A[5000000],B[5000000]; int n,m,next[5000000],res; void get_next(){ next[1]=0; int j=0; for(int i=2;i<=m;++i) { while(j>0&&B[j+1]!=B[i]) j=next[j]; if(B[j+1]==B[i]) j=j+1; next[i]=j; }}void kmp(){ res=0; int j=0; for(int i=1;i< 阅读全文

posted @ 2011-07-22 20:17 sysu_mjc 阅读(132) 评论(0) 推荐(0)

poj 3126 Prime Path

摘要: #include <iostream> //bfs#include <algorithm>#include<deque>using namespace std;bool prime(int n){ for(int i=2;i*i<=n;++i) if(n%i==0) return false; return true;}bool p[10000],visited[10000];struct node{ int n,c;}ans[10000];void init(){ for(int i=1000;i<10000;++i) if(prime(i)) 阅读全文

posted @ 2011-07-22 20:16 sysu_mjc 阅读(150) 评论(0) 推荐(0)

poj 3219 Binomial Coefficients

摘要: //判断C(n,k)的奇偶性//如果n!中因子2的个数大于k!和(n-k)!中因子2的个数之和,那么C(n,k)就是偶数,否则C(n,k)就是奇数。//而且计算N!中2的阶数,只要计算N/2+N/4……#include <iostream> using namespace std;int fac(int d) //计算d!中2的阶数,即因子2的个数{ int s=0; while((d=d>>1)!=0) s+=d; return s;}int f(int n,int k){ if(k==0||k==n) return 1; int a=fac(n),b=fac(n-k 阅读全文

posted @ 2011-07-22 20:16 sysu_mjc 阅读(166) 评论(0) 推荐(0)

poj 2752 Seek the Name, Seek the Fame

摘要: #include <iostream> //KMP算法using namespace std;char A[500000];int m,P[500000],res[500000];void get_next(){ P[1]=0; int j=0; for(int i=2;i<=m;++i) { while(j>0&&A[j+1]!=A[i]) j=P[j]; if(A[j+1]==A[i]) j=j+1; P[i]=j; }}int main(){ while(scanf("%s",A+1)!=EOF) { for(m=0;A[m+1 阅读全文

posted @ 2011-07-22 20:15 sysu_mjc 阅读(185) 评论(0) 推荐(0)

poj 2488 A Knight's Journey

摘要: #include<iostream> //dfs,注意字典顺序using namespace std;int ans[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; //按字典序排列int t,r,c,n,path[30][2],vis[30][30],rear;void dfs(int x,int y){ for(int i=0;i<8;++i) { int px=x+ans[i][0],py=y+ans[i][1]; if(px>0&&px<=r&& 阅读全文

posted @ 2011-07-22 20:14 sysu_mjc 阅读(123) 评论(0) 推荐(0)

poj 2533 Longest Ordered Subsequence

摘要: #include<iostream> //最长严格上升子序列(LIS)算法,时间复杂度为O(nlogn)using namespace std;int seq[1002]; //seq[i]是记录在所有最长严格上升子序列的长度为 i 之中,选取结束位置上最小的值int main(){ int n,p; cin>>n>>p; int rear=0; seq[++rear]=p; while(--n) { cin>>p; if(p>seq[rear]) //当p==seq[rear],不能压入... 阅读全文

posted @ 2011-07-22 20:14 sysu_mjc 阅读(105) 评论(0) 推荐(0)

poj 2392 Space Elevator

摘要: #include<iostream> //多重背包#include<algorithm>using namespace std;int dp[50000];struct node{ int h,a,c; bool operator<(const node& o) { return a<o.a; }}ans[405];int k;int main(){ cin>>k; for(int i=0;i<k;++i) cin>>ans[i].h>>ans[i].a>>ans[i].c; sort(ans,a 阅读全文

posted @ 2011-07-22 20:13 sysu_mjc 阅读(117) 评论(0) 推荐(0)

poj 2078 Matrix

摘要: #include<iostream> //搜索题using namespace std;int n,table[10][10],m,s,mx;void backtrack(int r){ if(r==n) { m=0; for(int j=0;j<n;++j) { s=0; for(int i=0;i<n;++i) { s+=table[i][j]; if(s>=mx) //剪枝 return ; } m=max(s,m); } mx=min(mx,m); } else { int t=n; while(t--) { int p=table[r][n-1]; fo 阅读全文

posted @ 2011-07-22 20:12 sysu_mjc 阅读(174) 评论(0) 推荐(0)

poj 2251 Dungeon Master

摘要: #include<iostream> //bfsusing namespace std;int vis[30][30][30],pos[4][2]={{-1,0},{0,1},{1,0},{0,-1}};char arr[30][30][30];int l,r,c;struct node{ int d,x,y,p;}ans[30000];int main(){ int sd,sx,sy; while(cin>>l>>r>>c&&l) { memset(vis,0,sizeof(vis)); for(int i=0;i<l;+ 阅读全文

posted @ 2011-07-22 20:12 sysu_mjc 阅读(120) 评论(0) 推荐(0)

poj 2063 Investment

摘要: // 题意: 开始时有一定数量的钱,有d种股票可以买,给出每种股票的价格和年收益,股票价格是1000的倍数// 如何使得投资的收益最大,即到最后手上的钱最多.#include<iostream> //完全背包 using namespace std;int dp[5000000];int main(){ int n,t,d,v,bond[50],interest[50]; cin>>n; while(n--) { cin>>v>>t>>d; for(int i=1;i<=d;++i) ... 阅读全文

posted @ 2011-07-22 20:11 sysu_mjc 阅读(145) 评论(0) 推荐(0)

poj 1887 Testing the CATCHER

摘要: #include<iostream> //最长不上升子序列using namespace std;int main(){ int cases=1,seq[10000],p; while(scanf("%d",&p),p!=-1) { int rear=0; seq[++rear]=p; while(scanf("%d",&p),p!=-1) { if(p<=seq[rear]) //当p==seq[rear],可以压入最长不上升子序列中 seq[++... 阅读全文

posted @ 2011-07-22 20:10 sysu_mjc 阅读(118) 评论(0) 推荐(0)

poj 1922 Ride to School

摘要: #include <iostream>using namespace std;int main(){ int n,v,t; while(cin>>n&&n) { int s=9999999; for(int i=0;i<n;++i) { cin>>v>>t; if(t>=0) { double a=4.5*3600/v; if(a-(int)a>0) a++; if(a+t<s) s=a+t; } } cout<<s<<endl; } return 0;}//找一个达到时间最少的,输 阅读全文

posted @ 2011-07-22 20:10 sysu_mjc 阅读(126) 评论(0) 推荐(0)

poj 1862 Stripies

摘要: #include<iostream>#include<vector>#include <algorithm>#include <cmath>using namespace std;int main(){ vector<double> col; int n;double a,e1,e2; cin>>n; for(int i=0;i<n;++i) { cin>>a; col.push_back(a); } sort(col.begin(),col.end()); for(int i=n-1;i>0;-- 阅读全文

posted @ 2011-07-22 20:09 sysu_mjc 阅读(98) 评论(0) 推荐(0)

poj 1844 Sum

摘要: #include<iostream>using namespace std;int main(){ int s,sum=0; cin>>s; for(int i=1;;++i) { sum+=i; if(sum>=s&&(sum-s)%2==0) { cout<<i<<endl; break; } } return 0;} bfs tle#include <iostream> //bfs tle#include <deque>using namespace std;int s;struct node{ 阅读全文

posted @ 2011-07-22 20:08 sysu_mjc 阅读(101) 评论(0) 推荐(0)

poj 1836 Alignment

摘要: // 题意:战士们站成一排,要求每个人都能看到在他左边或右边的所有人,// 只要有另外一人不比他矮,他就无法看到尽头了,问要至少出队多少人,即是求满足条件的最长子序列// 比如 1 2 3 4 4 3 2 1 就满足条件,但 1 2 3 3 3 2 1 不满足,因为中间有一个3不能看到左边或右边的尽头。// 分成两半,左半部分是最长严格上升子序列,范围[1-i],长度为Len1;// 而右半部分是最长严格下降子序列,范围[i+1-n],长度为Len2// 需要用 LIS算法 从前往后跑一遍,然后从后往前再跑一遍// 思路:枚举 i (1<=i<=n),利用LIS算法计算出Len1和 阅读全文

posted @ 2011-07-22 20:07 sysu_mjc 阅读(122) 评论(0) 推荐(0)

poj 1742 Coins

摘要: #include<iostream>using namespace std;int v[105],c[105],b[100005][2]; int main(){ int n,m,i,j,count,num; while(scanf("%d%d",&n,&m),n&&m) { memset(b,0,sizeof(b)); for(i=1;i<=n;i++) scanf("%d",&v[i]); for(i=1;i<=n;i++) scanf("%d",&c[i]) 阅读全文

posted @ 2011-07-22 20:06 sysu_mjc 阅读(119) 评论(0) 推荐(0)

poj 1631 Bridging signals

摘要: //最长严格上升子序列#include<iostream> //时间复杂度为O(nlogn)#include<algorithm>#include<vector>using namespace std;int main(){ int n,p,a; cin>>n; while(n--) { cin>>p>>a; vector<int> col; col.push_back(a); for(int i=1;i<p;++i) { cin>>a; if(a>col[col.size()-1]) 阅读全文

posted @ 2011-07-22 20:05 sysu_mjc 阅读(135) 评论(0) 推荐(0)

poj 1606 Jugs

摘要: #include <iostream> //bfs#include <deque>#include <string>using namespace std;struct node{ string path; int posa,posb;}ans[1000000];bool visited[1005][1005];string output[6]={"fill A","fill B","empty A","empty B","pour A B","po 阅读全文

posted @ 2011-07-22 20:04 sysu_mjc 阅读(135) 评论(0) 推荐(0)

poj 1491 Pi

摘要: #include <iostream> //水题#include <cmath>using namespace std;int gcd(int x,int y) { if (x==0||y==0) return max(x,y); int i=0,j=0; while (x&1==0) { ++i; x=x>>1; } while (y&1==0) { ++j; y=y>>1; } i=min(i,j); while(1) { if(x<y) swap(x,y); x-=y; if(x==0) return y<< 阅读全文

posted @ 2011-07-22 20:03 sysu_mjc 阅读(160) 评论(0) 推荐(0)

poj 1504 Adding Reversed Numbers

摘要: #include <iostream>#include <string>using namespace std;int main(){ int n,i,j; string str1,str2; cin>>n; while(n--) { cin>>str1>>str2; if(str1.size()<str2.size()) swap(str1,str2); int t=0; for(i=0;i<str2.size();++i) if(str1[i]+str2[i]-'0'+t>57) str1[i]= 阅读全文

posted @ 2011-07-22 20:03 sysu_mjc 阅读(104) 评论(0) 推荐(0)

poj 1503 Integer Inquiry

摘要: #include <iostream>#include <string>using namespace std;int compare(string str1, string str2){ while(!str1.empty()&&str1[0]=='0') { str1.erase(0,1); } while(!str2.empty()&&str2[0]=='0') { str2.erase(0,1); } if(str1.size() > str2.size()) //长度长的整数大于长度小的整数 阅读全文

posted @ 2011-07-22 20:02 sysu_mjc 阅读(97) 评论(0) 推荐(0)

poj 1455 Crazy tea party

摘要: #include <iostream>using namespace std;int main(){ int t,n,k; cin>>t; while(t--) { cin>>n; k=n/2; cout<<k*(k-1)/2+(n-k-1)*(n-k)/2<<endl; }}//http://blog.sina.com.cn/s/blog_6f71bea30100oxyz.html 阅读全文

posted @ 2011-07-22 20:01 sysu_mjc 阅读(93) 评论(0) 推荐(0)

poj 1154 LETTERS

摘要: #include<iostream> //dfsusing namespace std;bool visited[26];char board[100][100];int r,s,n;bool vis(int x,int y){ if(x<1||x>r||y<1||y>s) return true; return visited[board[x][y]-'A'];}void dfs(int x,int y,int num){ if(vis(x-1,y)&&vis(x+1,y)&&vis(x,y-1)&& 阅读全文

posted @ 2011-07-22 19:59 sysu_mjc 阅读(146) 评论(0) 推荐(0)

poj 1111 Image Perimeters

摘要: #include<iostream> //dfsusing namespace std;int r,c,mr,mc,num,ans[25][25],vis[25][25];int pos[4][2]={{-1,0},{0,1},{1,0},{0,-1}},slo[4][2]={{-1,1},{1,1},{1,-1},{-1,-1}};void dfs(int x,int y){ vis[x][y]=1; int px,py; for(int i=0;i<4;++i) { px=x+pos[i][0];py=y+pos[i][1]; if(ans[px][py]==0) num 阅读全文

posted @ 2011-07-22 19:58 sysu_mjc 阅读(140) 评论(0) 推荐(0)

poj 3624 Charm Bracelet

摘要: #include <iostream> //0-1背包using namespace std;int dp[20000],weigh[5000],val[5000];int main(){ int n,m,i,j; cin>>n>>m; for(i=0;i<n;++i) cin>>weigh[i]>>val[i]; for(i=n-1;i>=0;--i) for(j=m;j>=weigh[i];--j) dp[j]=max(dp[j],dp[j-weigh[i]]+val[i]); cout<<dp[m] 阅读全文

posted @ 2011-07-22 19:57 sysu_mjc 阅读(87) 评论(0) 推荐(0)

poj 3628 Bookshelf 2

摘要: #include <iostream>#include <algorithm>#include <functional>using namespace std;int arr[25],s,n,b,shelf[20000005];void dfs(int i){ s+=arr[i]; shelf[s]=1; if(s==b) return ; else if(s>b) s-=arr[i]; else { for(int j=i+1;j<n;++j) { dfs(j); if(s==b) return ; } s-=arr[i]; }}int mai 阅读全文

posted @ 2011-07-22 19:57 sysu_mjc 阅读(94) 评论(0) 推荐(0)

poj 3414 Pots

摘要: #include <iostream> //BFS#include <string>#include<deque>using namespace std;struct Node{ int f1,f2; string path; Node() { f1=f2=0; path=""; }}node[10000];int t,visited[105][105];int main(){ int a,b,c,e1,e2; string str; cin>>a>>b>>c; deque<Node> co 阅读全文

posted @ 2011-07-22 19:56 sysu_mjc 阅读(121) 评论(0) 推荐(0)

poj 3169 Layout

摘要: #include <iostream> //差分约束系统,Bellman-Ford算法using namespace std;const int inf=99999999;typedef struct Edge{ int u, v; // 起点,重点 int weight; // 边的权值}Edge;Edge edge[20020]; // 保存边的值int dist[1005]; // 结点到源点最小距离int nodenum, e,edgenum; // 结点数,边数,源点// 初始化图void init(){ // 输入结点数,边数 源点为V1 int ml,md; cin& 阅读全文

posted @ 2011-07-22 19:55 sysu_mjc 阅读(108) 评论(0) 推荐(0)

poj 3318 Matrix Multiplication

摘要: #include <iostream> //随机化算法using namespace std;int ma[505][505],mb[505][505],mc[505][505],n;int main(){ scanf("%d",&n); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) scanf("%d",&ma[i][j]); //cin 会TLE for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) scanf(" 阅读全文

posted @ 2011-07-22 19:55 sysu_mjc 阅读(147) 评论(0) 推荐(0)

poj 3051 Satellite Photographs

摘要: #include<iostream> //bfs#include<deque>#include<string>using namespace std;int w,h,m,s,visited[1005][85];char str[1005][85];deque<int> col;void bfs(int i,int j){ while(!col.empty()) { int i=col.front()/10000,j=col.front()%10000; col.pop_front(); s++; if(i-1>=0&&str 阅读全文

posted @ 2011-07-22 19:54 sysu_mjc 阅读(128) 评论(0) 推荐(0)

poj 3077 Rounders

摘要: #include <iostream>#include <string>using namespace std;int main(){ int n;string str; cin>>n; while(n--) { cin>>str; if(str.size()==1) { cout<<str<<endl;continue; } int tag=0; for(int i=str.size()-1;i>=1;--i) { if(str[i]>'4'||(tag==1&&str[i]= 阅读全文

posted @ 2011-07-22 19:54 sysu_mjc 阅读(100) 评论(0) 推荐(0)

poj 3009 Curling 2.0

摘要: #include <iostream> //dfs#include <deque>using namespace std;int w,h,square[100][100],c,num;void dfs(int x,int y){ if(c>num) return; int i,j; //不能设为全局变量 if(y>1&&square[x][y-1]!=1) //向左 { for(j=y;j>=1&&square[x][j]==0;--j); if(j>=1&&square[x][j]==3) { n 阅读全文

posted @ 2011-07-22 19:53 sysu_mjc 阅读(124) 评论(0) 推荐(0)

poj 2924 Gauß in Elementary School

摘要: #include <iostream>#include <string>using namespace std;int compare(string str1, string str2){ while(!str1.empty()&&str1[0]=='0') { str1.erase(0,1); } while(!str2.empty()&&str2[0]=='0') { str2.erase(0,1); } if(str1.size() > str2.size()) //长度长的整数大于长度小的整数 阅读全文

posted @ 2011-07-22 19:52 sysu_mjc 阅读(227) 评论(2) 推荐(0)

poj 2983 Is the Information Reliable?

摘要: #include <iostream> //差分约束系统,Bellman-Ford算法using namespace std;typedef struct Edge{ int u, v; // 起点,重点 int weight; // 边的权值}Edge;Edge edge[300000]; // 保存边的值int dist[1010]; // 结点到源点最小距离int nodenum, e,edgenum; // 结点数,边数,源点// 初始化图void init(){ // 输入结点数,边数 引入附加源点V0 for(int i=1; i<=nodenum; ++i) d 阅读全文

posted @ 2011-07-22 19:52 sysu_mjc 阅读(104) 评论(0) 推荐(0)

导航