2011年7月18日

最长上升子序列(LIS)算法

摘要: sicily 1060. Bridging Signals最长严格上升子序列#include<iostream> //最长严格上升子序列(LIS)算法,时间复杂度为O(nlogn)using namespace std;int seq[40010];int main(){ int cases,n,p; cin>>cases; while(cases--) { cin>>n>>p; int rear=-1; seq[++rear]=p; while(--n) { cin>>p; if(p>seq[rear]) //当p==seq[ 阅读全文

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

sicily 1060. Bridging Signals

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

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

sicily 1376. Monthly Expense

摘要: //题意:给你天数n,和每天需要花的钱,让你把这些天分成m份(每份都是连续的天),//要求每份的和尽量少,输出这个和。//一开始二分的上界为n天花费的总和(相当于分成1份),下界为每天花费的最大值(相当于分成n份),//然后二分,每次的mid值为(上界 + 下界)/ 2,然后根据mid值遍历n天花费,对花费进行累加,//每当超过mid值 份数++,看看这个mid值能把n天分成几份,如果份数大于m,表示mid偏小,下界 = mid + 1,//反之小于等于mid,上界 = mid,然后输出最后的mid值即可,复杂度为 O(nlogM)#include <iostream>#inclu 阅读全文

posted @ 2011-07-18 19:09 sysu_mjc 阅读(254) 评论(0) 推荐(0)

poj 3687 Labeling Balls

摘要: // 题意:有n个球, 重量分别为1-n, 给出一组(a,b)对,表示编号为a的小球比b小球要轻,小球的编号是1-n // 依次输出编号从1到n的球的重量, 要求编号小的球(即靠前的小球)的重量要尽量轻.// 反向建图,有向边heavy->light,逆拓扑排序,从重到轻逐一确定#include <iostream> //逆拓扑排序,从重到轻逐一确定using namespace std;int topo[201][201],in[201],w[201]; int main(){ int cases,n,m; cin>>cases; whi... 阅读全文

posted @ 2011-07-18 11:44 sysu_mjc 阅读(134) 评论(0) 推荐(0)

poj 2739 Sum of Consecutive Prime Numbers

摘要: #include <iostream>#include <math.h>using namespace std;int prime[2000];bool isprime(int n){ for(int i=2;i<=sqrt(n+0.0);i++) if(n%i==0)return false; return true;}int main(){ int i,j,end,s,top=0; for(i=2;i<10000;i++) { if(isprime(i)) prime[top++]=i; } int n,con; while(scanf("%d 阅读全文

posted @ 2011-07-18 11:44 sysu_mjc 阅读(140) 评论(0) 推荐(0)

poj 2719 Faulty Odometer

摘要: #include <string>#include<iostream>#include <math.h>using namespace std;int main(){ string str;int i,a,s; while(cin>>str&&str[0]!='0') { s=0; for(i=0;i<str.size();i++) { a=str[i]-48; if(a>4)a--; s+=a*pow(9,str.size()-i-1.0); } cout<<str<<&quo 阅读全文

posted @ 2011-07-18 11:43 sysu_mjc 阅读(117) 评论(0) 推荐(0)

poj 2593 Max Sequence

摘要: #include <iostream> //参照 POJ 2479using namespace std;#define MAX 100001int list[MAX],left_sum[MAX],right_sum[MAX]; //要声明为全局变量,如果在主函数内声明,会产生堆栈溢出int main(){ int n,i; while(scanf("%d",&n)&&n) { for(i=1;i<=n;i++) scanf("%d",&list[i]); left_sum[1]=list[1]; for 阅读全文

posted @ 2011-07-18 11:42 sysu_mjc 阅读(117) 评论(0) 推荐(0)

poj 2406 Power Strings

摘要: #include <iostream> //KMP算法using namespace std;char B[2000000]; int m,next[2000000]; 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; }}int main(){ while(scanf("%s",B+1)&&B[1]!=&# 阅读全文

posted @ 2011-07-18 11:41 sysu_mjc 阅读(129) 评论(0) 推荐(0)

poj 2479 Maximum sum

摘要: #include<iostream>#include<stdio.h>using namespace std;int num[50010],l[50010],r[50010];#define Min -9999999int main(){ int T,n,sum,tmp; cin>>T; while(T--) { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&num[i]); sum=0;tmp=Min; //l[i]表示[1,i]中最大的子段 阅读全文

posted @ 2011-07-18 11:41 sysu_mjc 阅读(116) 评论(0) 推荐(0)

poj 2140 Herd Sums

摘要: #include <iostream>using namespace std;int main(){ int i,n; int counts=0; scanf("%d",&n); for(i=1;i<=n/2;i+=2) //当i=1表示只由原数单独构成 if(n%i==0) counts++; if(n%2!=0) counts++; printf("%d\n",counts); return 0;}//只要是连续的数相加,则和 n 都可化成 一个奇数 i 乘以 某个数 a//比如: 1+2+3+4 = 5 * 2 ( 5 = 阅读全文

posted @ 2011-07-18 11:40 sysu_mjc 阅读(169) 评论(0) 推荐(0)

poj 2136 Vertical Histogram

摘要: #include <iostream>#include<string>using namespace std;int letter[27];int main(){ string str;int i,j; for( i=0;i<4;i++) { getline(cin,str); for( j=0;j<str.size();j++) if(str[j]>=65&&str[j]<=90) letter[str[j]-64]++; } int max=0; for(i=1;i<=26;i++) if(letter[i]>ma 阅读全文

posted @ 2011-07-18 11:39 sysu_mjc 阅读(147) 评论(0) 推荐(0)

poj 2105 IP Address

摘要: #include <iostream>#include <string>#include <math.h>using namespace std;void to_decimal(string s,int begin,int end){ int sum=0,j=7; for(int i=begin;i<=end;i++) sum+=pow(2.0,j--)*(s[i]-48); cout<<sum;}int main(){ string str; int t,p; cin>>t; while(t--) { cin>>s 阅读全文

posted @ 2011-07-18 11:38 sysu_mjc 阅读(98) 评论(0) 推荐(0)

poj 2081 Recaman's Sequence

摘要: #include <iostream> using namespace std;int list[500001];bool flag[10000000];int main(){ int n,top=0; fill(flag,flag+sizeof(flag),1); list[0]=0;flag[0]=0; while(scanf("%d",&n)&&n!=-1) { while(top<n) { if(list[top]-top-1>0&&flag[list[top]-top-1]) { top++; lis 阅读全文

posted @ 2011-07-18 11:37 sysu_mjc 阅读(97) 评论(0) 推荐(0)

poj 2039 To and Fro

摘要: #include <iostream>#include <string>using namespace std;int main(){ int interval,row,i,j; char str[210]; while(cin>>interval&&interval) { cin>>str; row=strlen(str)/interval; for(i=0;i<interval;i++) for(j=0;j<row;j++) { if(j%2==0) cout<<str[interval*j+i]; e 阅读全文

posted @ 2011-07-18 11:33 sysu_mjc 阅读(120) 评论(0) 推荐(0)

poj 2013 Symmetric Order

摘要: #include <iostream>#include <string>using namespace std;int main(){ char str[20][30]; int flag[20]; int n,t=1,i; while(cin>>n&&n) { cout<<"SET "<<t++<<endl; fill(flag,flag+20,1); for(int i=1;i<=n;i++) { cin>>str[i]; if(i%2==1) { flag[i]= 阅读全文

posted @ 2011-07-18 11:31 sysu_mjc 阅读(116) 评论(0) 推荐(0)

poj 1833 排列

摘要: #include <iostream>#include <algorithm>using namespace std;int main(){ int m,n,k,i,list[1024]; cin>>m;while(m--) { scanf("%d%d",&n,&k); for(i=0;i<n;i++) scanf("%d",&list[i]); while(k--) next_permutation(list,list+n); for(i=0;i<n;i++) printf(&qu 阅读全文

posted @ 2011-07-18 11:30 sysu_mjc 阅读(78) 评论(0) 推荐(0)

poj 1961 Period

摘要: #include <iostream> //KMP算法,参考poj 2406 Power Stringsusing namespace std;char B[2000000]; int m,next[2000000]; 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; }}int main(){ int num=1; while(scanf(& 阅读全文

posted @ 2011-07-18 11:30 sysu_mjc 阅读(108) 评论(0) 推荐(0)

poj 1716 Integer Intervals

摘要: //贪心,按区间的终止点(右边的点)从小到大排列,我们每次取区间最右边的点,使这个区间内至少有两个点在s中#include <iostream> #include <algorithm>using namespace std;struct node { int x;int y; bool operator<(const node& n)const { return y<n.y; }}interval[10001];int res[10001];int main(){ int n,i,j,ans,top=-1; cin>>n; for(i= 阅读全文

posted @ 2011-07-18 11:29 sysu_mjc 阅读(129) 评论(0) 推荐(0)

poj 1617 Crypto Columns

摘要: #include <iostream>#include <algorithm>#include <string>using namespace std;int main(){ char ch[12],cpy[12],letter[110]; int list[12],tag[12],i,j; while(cin>>ch&&strcmp(ch,"THEEND")!=0) { cin>>letter; strcpy(cpy,ch); sort(ch,ch+strlen(ch)); memset(tag, 阅读全文

posted @ 2011-07-18 11:28 sysu_mjc 阅读(161) 评论(0) 推荐(0)

poj 1611 The Suspects

摘要: // 题意: 一共有n个学生(编号0 至 n-1),m个组,一个学生可以同时加入不同的组。// 问有多少人与0号学生属于同一组#include <iostream> //并查集using namespace std;#define maxn 30002 int p[maxn],hei[maxn],n; int m,node[maxn];void init(){ for(int i=0;i<n;++i) //结点下标从0到n-1 { p[i]=i; hei[i]=0; }}int find(int x){ return... 阅读全文

posted @ 2011-07-18 11:27 sysu_mjc 阅读(127) 评论(0) 推荐(0)

poj 1595 Prime Cuts

摘要: #include <iostream>#include <math.h>using namespace std;bool isprime(int n){ for (int i=2;i<=sqrt((double)n);i++) { if(n%i==0) return false; } return true;}int list[500];int main(){ int N,C,i,j,top=0; list[0]=0; while(cin>>N>>C) { if(list[top]<N) { int t=list[top]; for( 阅读全文

posted @ 2011-07-18 11:26 sysu_mjc 阅读(121) 评论(0) 推荐(0)

poj 1579 Function Run Fun

摘要: #include <iostream>using namespace std;int rec[21][21][21];int w(int a,int b,int c){ if(a<=0||b<=0||c<=0) return 1; else if(a>20||b>20||c>20) return w(20,20,20); else if(rec[a][b][c]!=-1) return rec[a][b][c]; else { int s; if(a<b&&b<c) s=w(a,b,c-1)+w(a,b-1,c-1)- 阅读全文

posted @ 2011-07-18 11:25 sysu_mjc 阅读(113) 评论(0) 推荐(0)

poj 1564 Sum It Up

摘要: #include<iostream> //有重复的深搜剪枝 using namespace std; int mount,pre_value,mark;int a[12][2];void output(){ mark++; int flag=0; for(int i=0;i<mount;i++) { if(a[i][1]) { if(flag++) cout<<"+"; cout<<a[i][0]; } } cout<<endl; }void dfs(int sum,int now_value,int pre){ if( 阅读全文

posted @ 2011-07-18 11:24 sysu_mjc 阅读(186) 评论(0) 推荐(0)

poj 1552 Doubles

摘要: #include <iostream>#include <algorithm>using namespace std;int top,list[16];bool db(int i){ int j; for( j=i+1;j<top;j++) if(list[j]>=2*list[i]) break; if(list[j]==2*list[i]) return true; return false;}int main(){ int a,s,i; while(cin>>a&&a!=-1) { list[0]=a;top=1; whil 阅读全文

posted @ 2011-07-18 11:23 sysu_mjc 阅读(155) 评论(0) 推荐(0)

poj 1470 Closest Common Ancestors

摘要: #include <iostream> //并查集using namespace std;int main(){ int n,i,j; int parent[910],closest[910],vis_anc[910]; while(scanf("%d",&n)!=EOF) { int num,father,child; memset(parent,0,sizeof(parent[0])*(n+1)); for(i=1;i<=n;i++) { scanf("%d:(%d)",&father,&num); whil 阅读全文

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

poj 1328 Radar Installation

摘要: #include <iostream>#include <math.h>#include<algorithm>using namespace std;struct seg { double left;double right; bool operator<(const seg& a)const { return left<a.left; }}range[1001];int main(){ int n,d,i,t=1;double x,y;bool tag; while(cin>>n>>d) { if(n==0&am 阅读全文

posted @ 2011-07-18 11:18 sysu_mjc 阅读(164) 评论(0) 推荐(0)

poj 1032 Parliament

摘要: #include <iostream>using namespace std;int main(){ int s,list[1000],i,p=0,top=0; scanf("%d",&s); for(i=2;;i++) { if(p+i<=s) { p+=i; list[top++]=i; } else break; } p=s-p; int a=p/top,b=p%top; for(i=0;i<top;i++) list[i]+=a; for(i=top-1;i>top-1-b;i--) list[i]++; for(i=0;i&l 阅读全文

posted @ 2011-07-18 11:17 sysu_mjc 阅读(123) 评论(0) 推荐(0)

poj 1011 Sticks

摘要: #include <iostream>using namespace std;int sticks[200],visited[200];int flag,total;int t,seg,num;int cmp(const void* a,const void* b){ return (*(const int*)b)-(*(const int *)a);}void solve(int k,int sum,int cnt) //sum 是正在拼的木棍已经拼出的长度, k是从哪根碎木块开始拼,cnt是正在拼第几根木块{ if(num==cnt) flag=true; else if(su 阅读全文

posted @ 2011-07-18 11:16 sysu_mjc 阅读(173) 评论(0) 推荐(0)

poj 1007 DNA Sorting

摘要: #include <iostream>#include <algorithm>using namespace std;struct node{ char ch[52]; int unsort; bool operator<(const node& nd)const { return unsort<nd.unsort; }}dna[110];int us(char c[],int n){ int sum=0; for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if(c[i]>c[j]) sum 阅读全文

posted @ 2011-07-18 11:15 sysu_mjc 阅读(110) 评论(0) 推荐(0)

poj 3748 位操作

摘要: #include<iostream> //位运算using namespace std;int main(){ int r,x,y; scanf("%x,%d,%d",&r,&x,&y); r=r&(~(1<<x)); r=r|(1<<y); r=r|(1<<(y-1)); r=r&(~(1<<(y-2))); printf("%x\n",r); return 0;} 阅读全文

posted @ 2011-07-18 00:11 sysu_mjc 阅读(124) 评论(0) 推荐(0)

poj 3468 A Simple Problem with Integers

摘要: // 题意: 序列A[1-N], (1) C a b c 表示区间[a,b]里的数都加上c, (2) Q a b 查询区间[a,b]各个数的和#include <iostream> // 线段树, 修改和查询区间using namespace std;struct segment{ int l,r; __int64 sum,off; // sum记录该区间的和, 当覆盖范围为整个区间时,off记录所有的偏移量c, off初始化默认为0}tree[400000];int arr[100005],c;void built_tree(int n, int s,int... 阅读全文

posted @ 2011-07-18 00:10 sysu_mjc 阅读(153) 评论(0) 推荐(0)

poj 2606 Rabbit hunt

摘要: #include<iostream> //参照poj 1118 Lining Upusing namespace std;double c[201][2];//坐标数组double k[201];//斜率数组//QSORT的double类型比较函数int cmp_double( const void* a , const void* b ) { return *(double *)a > *(double *)b ? 1 : -1; } int main(){ int n,cnt,max,num; cin>>n; for(int i = 1;i <= n; 阅读全文

posted @ 2011-07-18 00:09 sysu_mjc 阅读(141) 评论(0) 推荐(0)

poj 3264 Balanced Lineup

摘要: // 题意: 给出一组数,输出区间最大值与最小值的差值#include <iostream> // 线段树,求区间最值问题using namespace std;struct segment{ int high,low; //记录该区间的最大值和最小值}table[200000];int arr[50005],MAX,MIN;void built_tree(int n,int s,int t) //建树,其中第n个区间的范围是[s,t]{ if(s==t) table[n].high=table[n].low=arr[s]; e... 阅读全文

posted @ 2011-07-18 00:09 sysu_mjc 阅读(132) 评论(0) 推荐(0)

poj 2142 The Balance

摘要: #include <iostream>using namespace std;int x0,y0,g;void extend_gcd(int a,int b){ if(b==0) { x0=1;y0=0;g=a; } else { extend_gcd(b,a%b); int tmp=x0; x0=y0; y0=tmp-(a/b)*y0; }}int main(){ int a,b,d,x,y;bool tag; while(cin>>a>>b>>d&&a) { int res1,res2;tag=1; if(a<b) { 阅读全文

posted @ 2011-07-18 00:05 sysu_mjc 阅读(181) 评论(0) 推荐(0)

poj 2182 Lost Cows

摘要: /* 题意: 有n个数,从1到n,打乱顺序, 现输入n-1个数,第i个数表示序列中第1到i-1的数比第i个数小的个数.要求输出该序列 输入: 5 1 2 1 0 输出: 2 4 5 3 1*/#include <iostream> //线段树using namespace std;struct node{ int l,r,num; //num记录[l,r]中还剩余多少人尚未被确定}tree[40000];void build(int n,int s,int t){ tree[n].l=s; tre... 阅读全文

posted @ 2011-07-18 00:05 sysu_mjc 阅读(195) 评论(0) 推荐(0)

poj 1799 Yeehaa!

摘要: #include <iostream>#include <math.h>const double pi=3.1415926535898;using namespace std;int main(){ int t,i;double R,n; cin>>t; for(i=1;i<=t;i++) { cin>>R>>n; printf("Scenario #%d:\n",i); printf("%.3lf\n\n",R*sin(pi/n)/(1+sin(pi/n))); } return 0;} 阅读全文

posted @ 2011-07-18 00:03 sysu_mjc 阅读(183) 评论(0) 推荐(0)

poj 2115 C Looooops

摘要: // 题意: 解同余方程 Cx≡B-A ( mod 2^k ) ,求出x的最小解#include<iostream>#include <math.h>using namespace std;long long x_0,y_0,q; void extend_eulid(long long a,long long b) //x_0*a+y_0*b=gcd(a,b)=q{ if(b==0) { x_0=1;y_0=0;q=a; } else { extend_eulid(b,a%b); ... 阅读全文

posted @ 2011-07-18 00:03 sysu_mjc 阅读(185) 评论(0) 推荐(0)

poj 1363 Rails

摘要: #include<iostream> #include<stack> using namespace std; int n,target[1001],temp; int main() { while(cin >> n&&n) { while(cin >> temp&&temp) { stack<int> s; int A = 1,B = 1; //A是有顺序的车厢号,B是栈堆指针 target[1] = temp; for(int i = 2;i <= n;++i) { cin >> 阅读全文

posted @ 2011-07-18 00:01 sysu_mjc 阅读(155) 评论(0) 推荐(0)

poj 1330 Nearest Common Ancestors

摘要: #include<iostream>#include<memory.h>using namespace std;int node[10001];int path1[10001],path2[10001];void parent(int list[],int s,int& top){ top=-1; do { list[++top]=s; s=node[s]; } while (s);}int main(){ int t,n,i,j,p,c; cin>>t; while(t--) { cin>>n; memset(node,0,sizeof 阅读全文

posted @ 2011-07-18 00:00 sysu_mjc 阅读(120) 评论(0) 推荐(0)

导航