随笔分类 -  uva

摘要:汉诺塔问题:首先得记住一个经典的结论:把i个盘子移动到另外一个柱子上要花2^i-1步然后这个题:首先从底往上找到要移动的编号最大的盘子,把上面所有的移动在临时的柱子,然后把那个编号最大的移动到目标状态,最后把前面移到临时柱子上的移到目标状态(循环递归);利用前面的那个结论递归就可以了!代码: 1 #include 2 #define maxn 66 3 #define ll long long 4 using namespace std; 5 6 ll f(int *p,int i,int final) 7 { 8 if(i==0)return 0; 9 if(p[i]==f... 阅读全文
posted @ 2013-10-28 21:06 Yours1103 阅读(172) 评论(0) 推荐(0)
摘要:一个简单的线段树;被我看错题了,浪费一个半小时; 1 #include 2 #define maxn 500005 3 using namespace std; 4 5 struct tree 6 { 7 int l,r,value; 8 tree *right,*left; 9 }tr[maxn];10 int nonocount;11 int ans;12 13 void build(tree *rt,int l,int r)14 {15 rt->l=l;16 rt->r=r;17 if(l==r)18 {19 scanf(... 阅读全文
posted @ 2013-10-26 17:53 Yours1103 阅读(166) 评论(0) 推荐(0)
摘要:字典树,可惜比赛的时候有两句话写倒了;害得我调了一个小时;今天不宜做题 = =代码: 1 #include 2 #include 3 #define maxn 2600009 4 using namespace std; 5 6 struct node 7 { 8 bool flag; 9 int cnt;10 node *a[26];11 } no[maxn];12 13 char s[100];14 int ans,nonocount;15 node *newnode()16 {17 node *p=no+nonocount++;18 p->fla... 阅读全文
posted @ 2013-10-26 17:50 Yours1103 阅读(291) 评论(0) 推荐(0)
摘要:简单题,二分就行; 1 #include 2 #include 3 #define pi acos(-1.0) 4 #define eps 0.000001 5 #define maxn 10009 6 using namespace std; 7 double area[maxn]; 8 9 int main()10 {11 int t,n,f,ri;12 double r=-1;13 scanf("%d",&t);14 while(t--)15 {16 scanf("%d%d",&n,&f);17 f=f+1;18 ... 阅读全文
posted @ 2013-10-26 10:57 Yours1103 阅读(177) 评论(0) 推荐(0)
摘要:代码: 1 #include 2 using namespace std; 3 4 int getans(int x) 5 { 6 int ans=1; 7 while(x>1) 8 { 9 x>>=1;10 ans++;11 }12 return ans;13 }14 15 int main()16 {17 int n;18 while(scanf("%d",&n)!=EOF)19 printf("%d\n",getans(n));20 return 0;21 }View Cod... 阅读全文
posted @ 2013-10-26 10:31 Yours1103 阅读(110) 评论(0) 推荐(0)
摘要:这个题的突破点就在于蚂蚁不能够穿过对方,故相对位置不变;另外,又可以把蚂蚁看成运动方向不变;代码: 1 #include 2 #include 3 using namespace std; 4 #define maxn 10005 5 6 char dir[][10]={"L","Turning","R"}; 7 8 int order[maxn]; 9 10 struct ant11 {12 int id,p,d;13 bool operatorl)puts("Fell off");46 else printf( 阅读全文
posted @ 2013-10-25 23:47 Yours1103 阅读(159) 评论(0) 推荐(0)
摘要:这个题的方法很巧妙,首先将整个圆分成(m+n)份,这样移动后的点都是在整数值上;所以只要计算在这样的分法下原来的坐标就行了;代码: 1 #include 2 #include 3 using namespace std; 4 5 int main() 6 { 7 int m,n; 8 { 9 while(scanf("%d%d",&n,&m)!=EOF)10 {11 double ans=0;12 for(int i=1;i<n;i++)13 {14 ... 阅读全文
posted @ 2013-10-25 22:49 Yours1103 阅读(165) 评论(0) 推荐(0)
摘要:这个题目的证明挺美的;把分金币问题变成了一个数轴上点的距离问题;代码: 1 #include 2 #include 3 #define ll long long 4 #define maxn 1000009 5 using namespace std; 6 7 ll a[maxn],b[maxn],sum,m; 8 9 int main()10 {11 int n;12 while(scanf("%d",&n)!=EOF)13 {14 sum=0;15 for(int i=0;i<n;i++){scanf("%lld",&a[i]) 阅读全文
posted @ 2013-10-25 13:12 Yours1103 阅读(173) 评论(0) 推荐(0)
摘要:水题怡情 1 #include 2 #include 3 #define maxn 1005 4 using namespace std; 5 struct war 6 { 7 int a,b; 8 bool operatort.b;11 }12 }wa[maxn];13 14 int main()15 {16 int n,ca=1;17 while(scanf("%d",&n)&&n)18 {19 for(int i=0;i<n;i++)scanf("%d%d",&wa[i].a,&wa[i].b);20 阅读全文
posted @ 2013-10-25 13:00 Yours1103 阅读(146) 评论(0) 推荐(0)
摘要:做一两个水题怡情一下! 1 #include 2 #include 3 #define maxn 20009 4 using namespace std; 5 int a[maxn],b[maxn]; 6 7 int main() 8 { 9 int n,m;10 while(scanf("%d%d",&n,&m)&&(n+m))11 {12 for(int i=0;im){puts("Loowater is doomed!");continue;}15 sort(a,a+n);16 sort(b,b+m);17 ... 阅读全文
posted @ 2013-10-25 12:47 Yours1103 阅读(120) 评论(0) 推荐(0)
摘要:RMQ问题;采用游程编码;代码: 1 #include 2 #include 3 #include 4 #define maxn 100009 5 using namespace std; 6 7 int d[maxn][30],value[maxn],cnt[maxn],num[maxn],left[maxn],right[maxn]; 8 9 void RMQ_init(int n)10 {11 for(int i=1; ir)return 0;20 int k=0;21 while(1<<(k+1)<=r-l+1)k++;22 return max(d... 阅读全文
posted @ 2013-10-24 20:58 Yours1103 阅读(247) 评论(0) 推荐(0)
摘要:树状数组,把他们的技能值作为轴;首先按照编号从小到大插入值,这样就可以得到,技能值比当前小的人数;然后按照编号从大到小再插一遍;代码: 1 #include 2 #include 3 #define maxn 20005 4 using namespace std; 5 6 int a[maxn],c[100010]; 7 int l[maxn],r[maxn]; 8 int lowbit(int x){return x&-x;} 9 void add(int x,int d){while(x0){ret+=c[x],x-=lowbit(x);}14 return ret;15 }1. 阅读全文
posted @ 2013-10-24 18:45 Yours1103 阅读(175) 评论(0) 推荐(0)
摘要:用两个优先队列来实现,因为队列只能从一头出去;所以维护一个数组,来标记这个队列的已经出列而另外一个队列没有出列的元素;到时候再把他们删了就行; 1 #include 2 #include 3 #include 4 #define maxn 1000009 5 using namespace std; 6 7 priority_queue,greater >gq; 8 priority_queue,less >lq; 9 int numg[maxn],numl[maxn];10 int main()11 {12 int n,x,k;13 while(scanf("%d&qu 阅读全文
posted @ 2013-10-24 17:28 Yours1103 阅读(204) 评论(0) 推荐(0)
摘要:链表的应用;le[i]表示第i个元素左边的那个元素的标号;ri[i]表示第i个元素右边的那个元素的标号;代码: 1 #include 2 #include 3 #define maxn 100009 4 using namespace std; 5 6 char s[maxn]; 7 int le[maxn],ri[maxn]; 8 9 int main()10 {11 while(gets(s+1)!=NULL)12 {13 le[0]=ri[0]=0;14 for(int i=1; s[i]; i++)15 {16 ... 阅读全文
posted @ 2013-10-24 16:43 Yours1103 阅读(240) 评论(0) 推荐(0)
摘要:带权值的并查集的应用;代码: 1 #include 2 #include 3 #include 4 #include 5 #define maxn 20005 6 using namespace std; 7 8 int f[maxn]; 9 int d[maxn];10 int t,n,x,y;11 char s[5];12 13 int find(int x)14 {15 if(f[x]!=x)16 {17 int r=find(f[x]);18 d[x]+=d[f[x]];19 return f[x]=r;20 }... 阅读全文
posted @ 2013-10-24 12:35 Yours1103 阅读(148) 评论(0) 推荐(0)
摘要:并查集的简单应用:代码: 1 #include 2 #define maxn 100005 3 using namespace std; 4 int f[maxn]; 5 int find(int x){return x==f[x]?x:f[x]=find(f[x]);} 6 int main() 7 { 8 int x,y; 9 while(scanf("%d",&x)!=EOF)10 {11 for(int i=0;i<maxn;i++)f[i]=i;12 int cnt=0;13 while(x!=-1)14 ... 阅读全文
posted @ 2013-10-24 10:50 Yours1103 阅读(129) 评论(0) 推荐(0)
摘要:多路并归问题:代码: 1 #include 2 #include 3 #include 4 #define maxn 760 5 using namespace std; 6 7 struct node 8 { 9 int s,b;10 node(int s,int b):s(s),b(b) {}11 bool operatort.s;14 }15 };16 17 void merge(int *a,int *b,int *c,int n)18 {19 priority_queueq;20 for(int i=0; i<n; i++)21 ... 阅读全文
posted @ 2013-10-24 10:13 Yours1103 阅读(195) 评论(0) 推荐(0)
摘要:简单的优先队列的应用;代码: 1 #include 2 #include 3 using namespace std; 4 5 struct node 6 { 7 int num; 8 int ti; 9 int period;10 bool operatort.num;13 return ti>t.ti;14 }15 };16 priority_queueq;17 char s[20];18 int main()19 {20 int m;21 node a;22 while(scanf("%s",&s)&&s[... 阅读全文
posted @ 2013-10-24 09:49 Yours1103 阅读(157) 评论(0) 推荐(0)
摘要:map的用法:代码: 1 #include 2 #include 3 #include 4 #define maxn 1000009 5 using namespace std; 6 7 vectorve[maxn]; 8 map >mp; 9 int n,m,x,y;10 int main()11 {12 while(scanf("%d%d",&n,&m)!=EOF)13 {14 mp.clear();15 for(int i=0;i();19 mp[x].push_back(i+1);20 }21 ... 阅读全文
posted @ 2013-10-23 23:15 Yours1103 阅读(151) 评论(0) 推荐(0)
摘要:题目很简单,就是不知道为啥总是runtime error;后来发现我是先判断的y!=p.front();应该先判断是否为空;哎!今天果然不宜A题啊!代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 priority_queuepq; 6 queueq; 7 stacks; 8 bool flag[4]; 9 int main()10 {11 int n,x,y;12 while(scanf("%d",&n)!=EOF)13 {14 while(!q.empty())q.pop();... 阅读全文
posted @ 2013-10-23 22:57 Yours1103 阅读(190) 评论(0) 推荐(0)