比赛链接:https://vjudge.net/contest/155289#overview 密码:nyist
C、穿越沙漠:http://acm.fzu.edu.cn/problem.php?pid=1076
题意:一辆吉普车来到x公里宽的沙漠边沿A点,吉普车的耗油量为1升/公里,总装油量为500升,假设在沙漠边沿A点有充足的汽油可供使用,那么吉普车从A点穿过这片沙漠到达终点B,至少要耗多少升油.
分析:每装500升油,可以跑500/ans公里,其中ans是往返次数.我们倒着来推,求出至少要往返多少次?把存油点放在什么位置?无论装多少,他的耗油量都是1升/公里,所以每次装尽可能多的油,但最后一次可能不需要装满这时我们就可以倒过来想,让它的路程尽可能的短,把存油点离起始点尽可能的近,就是这样.
Code:
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<stdlib.h>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const int maxn=100005;
const double eps=0.0000001;
const int mod=1000000007;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double n;
scanf("%lf",&n);
int ans=0;
while(n>0)
{
ans++;//往返次数
n-=500.0/(2*ans-1);
}
double res=500.0*ans+(2*ans-1)*n;
printf("%.3lf\n",res);
}
return 0;
}
D、洗牌问题:http://acm.fzu.edu.cn/problem.php?pid=1062
打表找规律,比赛时做了差不多两个小时也没找到规律,思路是对的,找规律的方向是错的,我当时只是想第一个位置是几,而没有想1在第几个位置,然而找了半天也没找到,苦逼了。。。
题意很明白,问多少次洗牌之后还能回到原来的顺序,当时我只以为按递增或递减的顺序就行了,其实要和原来的顺序一样,只能是递增的。然后就是找规律:当1的位置pos<=n时,下一次洗牌就变成2*pos,
当1的位置pos>n时,下一次洗牌就变成2*(n-pos)-1;
当然第一次洗牌后1就在2的位置,当1再次回到1的位置的时候就找到结束了。。。就是这样,直接暴力。
CODE:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<stdlib.h>
typedef long long LL;
using namespace std;
const int maxn=200005;
int a[maxn],b[maxn];
int p[maxn];
int judge(int x)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int count=0;
for(int i=1; i<=2*x; i++)
a[i]=i;
while(true)
{
count++;
for(int i=1; i<=x; i++)
{
b[2*i-1]=a[i+x];
b[2*i]=a[i];
}
for(int i=1; i<=2*x; i++)
a[i]=b[i];
int ans=0;
for(int i=1;i<=2*x;i++)
if(a[i]==1)
cout<<i<<" ";
// cout<<a[i]<<" ";
// cout<<endl;
for(int i=2; i<=2*x; i++)
{
if(abs(a[i]-a[i-1])==1)
ans++;
}
if(ans==2*x-1)
break;
}
//cout<<count<<"*****"<<endl;
return count;
}
int main()
{
int n;
// for(int i=1;i<=100000;i++)
// p[i]=init(i);
while(~scanf("%d",&n))
{
//printf("%d\n",judge(n));
int pos=2,ans=1;
while(pos!=1)
{
if(pos<=n)
pos*=2;
else
pos=(pos-n)*2-1;
// cout<<pos<<" ";
ans++;
}
printf("%d\n",ans);
}
}
E.Proud Merchantshttp://acm.hdu.edu.cn/showproblem.php?pid=3466
01背包+贪心:要对01背包有深入的了解,然后弄清楚里面的关系,找到状态转移和贪心策略。
分析:不考虑当前钱数q的限制时,就是普通的01背包,状态转移为dp[j]=max(dp[j],dp[j-c[i].p]+c[i].w);
如果有了这个限制,当前钱数为m-(j-c[i].p);j-c[i].p为此次转移的花费;只有当m-(j-c[i].p)>c[i].q&&j-c[i].p>=0,才能实现状态转移;所以:c[i].p<=j<m+c[i].p-c[i].q;即:更新区间为(c[i].p,m+c[i].p-c[i].q)
根据01背包的性质,就是说前面更新的范围应该比后面大。所以就是p1-q1>p2-q2;
即按照p-q从大到小排序,或者q-p按照从小到大排序才能保证后面的能更新到不丢失状态。
CODE:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<stdlib.h>
typedef long long LL;
using namespace std;
const int maxn=200005;
struct node
{
int p,q,w;
}c[5005];
int dp[5005];
bool cmp(node aa,node bb)
{
return aa.p-aa.q>bb.p-bb.q;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
scanf("%d%d%d",&c[i].p,&c[i].q,&c[i].w);
sort(c+1,c+1+n,cmp);
for(int i=1;i<=n;i++)
{
for(int j=m;j>=c[i].q;j--)
{
dp[j]=max(dp[j],dp[j-c[i].p]+c[i].w);
//cout<<dp[j]<<" ";
}
//cout<<endl;
}
cout<<dp[m]<<endl;
}
}
B.Number lengthshttp://acm.fzu.edu.cn/problem.php?pid=1050
题意就是求N!有多少位数字,就是这么简单......以前好像见过这种题,然后就很快A了
N是十进制数字,它的位数就是log10(N)+1,比如说N=100,则log10(100)=2,它的位数就是2+1=3位。然后
N!=1*2*3.....*N,则log10(N!)=log10(1*2*3*...*N)=log10(1)+log10(2)+log10(3)+...+log10(N);就是利用这个原理求N! 有多少位。
CODE:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
typedef long long LL;
using namespace std;
const int maxn=1000005;
double a[maxn];
void init()
{
memset(a,0,sizeof(a));
a[0]=0;
for(int i=1;i<maxn;i++)
a[i]=a[i-1]+log10(i);
}
int main()
{
int n;
init();
while(~scanf("%d",&n))
{
// double ans=0;
// for(int i=1;i<=n;i++)
// {
// double res=log10(i);
// ans+=res;
// }
printf("%d\n",(int)(a[n]+1));
}
}
整个比赛的状态不是很好,没能很投入的去比,排名当然也不理想,再接再厉吧。