Codeforces Testing Round #3

好丢人的比赛啊。。一个都没对。。无语中。。考虑不全啊。。

http://codeforces.com/contest/134/problem/A

本来才开始考虑小数来,可最后还是在抓时间结构wa了。。就是很简单的求平均数然后在查找的过程。注意平均数可能是小数。。

http://codeforces.com/problemset/problem/134/B

题意:给出原始数对(1,1)到(n,x)变换求最小的步数。改变如下:

 (a,b)-->(a + b, b) or (a, a + b)才开始做的时候以为直接用a+b来替代a,b中较小的那个然后不断这样下去直到出现n。这样肯定保证的步数最小,才是样例都过了二话没说就交了。在还没看完第三题就被hacking了,,无语啊。。自己根本就没有考虑全面。最后听强哥讲了讲音乐明白,我这笨脑子啊。

当出现(n,x)的时候n肯定要比x大的,所以枚举所有的(n,i)1<=i<=n-1:找出需要步数最小的来就ok(因为(n,i)与(i,n)是相同的最小步数 ),枚举是关键,

对于(a,b),1:如果a>b ,a肯定是由x+b来的(x是上一层的数),也就是b属于上一层;2:如果a<b,同理。。然后往后枚举。。

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int inf = 1000007;

int solve(int a,int b){
if (b==1) return a-1;
if (a%b==0) return inf;//不可能出现a==b&&b!=1
return a/b+solve(b,a%b);
//a/b代表a-b的个数solve(b,a%b)是当a<b是的情况再递归求解
}
int n;
int main()
{
scanf("%d",&n);
if (n==1)
{
puts("0");
return 0;
}
int res=inf;
for (int i=1;i<n;i++)
{
int tmp=solve(n,i);
if (tmp<res) res=tmp;
}
printf("%d\n",res);
return 0;
}



posted @ 2011-12-03 20:09  E_star  阅读(198)  评论(0编辑  收藏  举报