www.bersaty.com

随笔分类 -  ACM

摘要:个仅由红、白、蓝这三种颜色的条块组成的条块序列。请编写一个时间复杂度为O(n)的算法,使得这些条块按蓝、白、红的顺序排好例如: R,B,B,W,W,B,R,B,W排序后:B,B,B,B,W,W,W,R,Rvoid Three_color_flag(int color[], int n ) //排序为蓝,白,红 { int white = 0 ; int blue = 0 ; int red = n - 1 ; while(white <= red) { if(color[white] == WHITE)... 阅读全文
posted @ 2012-02-26 00:33 bersaty 阅读(997) 评论(0) 推荐(0)
摘要:// 马走棋盘.cpp : Defines the entry point for the console application.//#include "stdafx.h"#includeint maze[8][8],n,x,y; //x,y开始坐标,n维数 int mx[8]={-2,-2,-1,1,2,2,1,-1};//方向 int my[8]={-1,1,2,2,1,-1,-2,-2};int cnt=0;//记录路径数量 bool check(int x,int y){ return (maze[x][y]==0&&x>=0&&am 阅读全文
posted @ 2012-02-23 20:00 bersaty 阅读(514) 评论(0) 推荐(0)
摘要:void quicksort(int arry[],int left,int right){ int i=left,j=right,key=arry[left]; while(i=key) j--; arry[i]=arry[j]; while(i<j&&arry[i]<=key) i++; arry[j]=arry[i]; } arry[i]=key; if(i-left) quicksort(arry,left,i-1); if(right-i) quicksort(arry,i+1,right);} 阅读全文
posted @ 2012-02-23 18:18 bersaty 阅读(207) 评论(0) 推荐(0)
摘要:int maze[m+1][n+1];//迷宫数组int sx[m*n+1]; //保存路径int sy[m*n+1];int dx[4]={0,0,1,-1}; //方向 int dx[4]={1,-1,0,0};void gomaze(int step,int x,int y) //x,y当前坐标,step要走第几步{ int i; sx[step]=x;sy[step]=y;//保存当前坐标 if(x==m&&y==n)//走到出口,打印路径 { for(i=1;i=1&&x=1)//如果可行,递归继续 gomze(step+1,nx,ny); }} 阅读全文
posted @ 2012-02-23 18:11 bersaty 阅读(292) 评论(0) 推荐(1)
摘要:题目http://acm.hdu.edu.cn/showproblem.php?pid=1166度娘的 树状数组 http://baike.baidu.com/view/1420784.htm弄了一个下午,算是明白了点看了一些牛人的代码后的翻版。。#includeusing namespace std;int tr[500005],n;int lowbit(int a){return a&-a;}//找位置 int quy(int i) //计算前i项和 { int ans=0; for(;i>0;i-=lowbit(i)) ans+=tr[i]; retu... 阅读全文
posted @ 2012-02-06 21:30 bersaty 阅读(286) 评论(0) 推荐(0)
摘要:Catch That Cowhttp://poj.org/problem?id=3278RE了N次,终于过了,数组开小了= =。。顺便联系队列#includestruct point { int pos;}queue[200001];int head=0,tail=0;bool mat[200001]={0};int step[200001];bool is_empty(){ return head==tail; }point dequeue(){ return queue[head++];}void enqueue(point p){ queue[tail++]... 阅读全文
posted @ 2012-02-03 16:37 bersaty 阅读(206) 评论(0) 推荐(0)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2544继续研究,dijkstra 和 bellmanford 算法#includeint main(){ int n,m,a,b,c; //freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF&&(n||m)) { int maze[101][101]={0}; while(m--) { scanf("%d%d%d",&a,& 阅读全文
posted @ 2012-02-01 17:33 bersaty 阅读(232) 评论(0) 推荐(0)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1014题目看了好久,终于看完了,发现是群论里的,离散也差多忘了。。。说白了就是求生成元,求mod N的生成元,即生成元与N互质就OK了#include<iostream>int gcd(int a,int b){ if(!b) return a; return gcd(b,a%b);}int main(){ int s,m; while(scanf("%d%d",&s,&m)!=EOF) { if(gcd(s,m)==1) printf("%10d%1 阅读全文
posted @ 2012-02-01 10:46 bersaty 阅读(1401) 评论(0) 推荐(0)
摘要:网上找的,慢慢研究下View Code 1 #include <cmath> 2 #include <complex> 3 #include <cstring> 4 using namespace std; 5 6 const double PI = acos(-1); 7 typedef complex<double> cp; 8 typedef long long int64; 9 10 const int N = 1 << 16; 11 int64 a[N], b[N], c[N << 1]; 12 13 void 阅读全文
posted @ 2011-12-08 19:39 bersaty 阅读(780) 评论(0) 推荐(0)
摘要:DieIng五一要去旅游,旅游区的景点道路分布如图:欣赏景点的道路为东西走向,每条道路有DieIng对它的喜爱值;南北走向为林间小道,供休息用。 由于五一游客较多,旅游区规定欣赏景点的道路只能单向行走,自东向西走;林间小道可双向行走。 DieIng要你帮他设计路线(可以从任意点开始,任意点结束),使得他能玩得最high。(喜爱值的总和最大) Input 第一行是两个整数N(0 using namespace std;long int s[1000000];int main(){ long int i,j,sum,max,m,n,a,k; while(scanf("%ld %ld&qu 阅读全文
posted @ 2011-12-05 19:15 bersaty 阅读(226) 评论(0) 推荐(0)
摘要:主要是处理多个集合的问题,图论等,像欧拉回路主要代码 像hdoj的1878,NYoj 的一笔画问题等都可以处理主要代码int find(int x) //查找根节点{ int r=x; while(r!=set[r]) r=set[r]; return r;}void union(int x,int y) //合并{ x=find(x); y=find(y); if(x>y) set[x]=y; else set[y]=x;} 阅读全文
posted @ 2011-12-05 19:07 bersaty 阅读(167) 评论(0) 推荐(0)
摘要:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?输入n,接着n行,nusing namespace std;int main(){ int n,a[210],t[210]={0},i,j; cin>>n; for(i=1;i>a[j]; for(j=i;j>=1;j--) t[j]=a[j]+(t[j-1]>t[j]? t[j-1]:t[j]); } j=0; for(i=1;i<n;i++) if(j<t[i]) ... 阅读全文
posted @ 2011-11-14 23:40 bersaty 阅读(313) 评论(0) 推荐(0)
摘要:编辑器加载中...#include using namespace std;#define N 7//物品数量#define S 20//要求背包重量int W[N+1]={0,1,4,3,4,5,2,7};//各物品重量,W[0]不使用。。。int knap(int s,int n)//s为剩余重量,n为剩余可先物品数。。{ if(s==0) return 1;//return 1 means success.. if(s0&&n<1)) return 0;//如果s<0或n<1则不能完成 if(knap(s-W[n],n-1... 阅读全文
posted @ 2011-10-28 18:33 bersaty 阅读(216) 评论(0) 推荐(0)
摘要:现有21根火柴,两人轮流取,每人每次可取走1- 4根,不可多取,也不能不取,谁取最后一根火柴则谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。从前往后推(人) (机)1 2~56 7~101112~1516 17~2021只要人按1,6,11,16,21走,计算机就必定能赢#includeint main(){ int m,n=21; while(1) { printf("How many sticks do you wish to take(1~4)?"); scanf("%d",&m); n-=m; if( 阅读全文
posted @ 2011-10-27 21:11 bersaty 阅读(318) 评论(0) 推荐(0)
摘要:水~~不过有些小问题这是AC代码:#include<iostream>#include<string.h>using namespace std;int main(){ int n,y; char s[500]; while(cin>>n) while(n--){ scanf("%s",s); y=1; int k=strlen(s); for(int i=0;i<=k/2;i++) { if(s[i]!=s[k-i-1]) y=0; } cout<<(y?"yes":"no")& 阅读全文
posted @ 2011-07-19 19:23 bersaty 阅读(178) 评论(0) 推荐(0)
摘要:水~~找规律#include<iostream>using namespace std;int cow(int n){ if(n>4) return cow(n-1)+cow(n-3); else return n;}int main (){ int n; while(cin>>n&&n) printf("%d\n",cow(n)); return 0;} 阅读全文
posted @ 2011-06-27 21:51 bersaty 阅读(193) 评论(0) 推荐(0)
摘要:2000~2007//2000 http://acm.hdu.edu.cn/showproblem.php?pid=2000#include<stdio.h>#include<string.h>void main(){ char str[4],t; int i; while(scanf("%s",str)!=EOF) { for(i=0;i<2;i=i++) { if(str[i]>str[i+1]) { t=str[i]; str[i]=str[i+1]; str[i+1]=t; } } for(i=0;i<2;i=i++) { 阅读全文
posted @ 2011-06-27 20:43 bersaty 阅读(266) 评论(0) 推荐(0)
摘要:初看到这题就想到fibonacci数,算了几个,果然!!#include<iostream>using namespace std;int main(){ int n,m,a[45],i; a[1]=0; a[2]=1; a[3]=2; for(i=4;i<=40;i++) a[i]=a[i-1]+a[i-2]; while(scanf("%d",&n)!=EOF) { while(n--) { scanf("%d",&m); printf("%d\n",a[m]); } } return 0;} 阅读全文
posted @ 2011-06-20 17:22 bersaty 阅读(367) 评论(0) 推荐(1)