您的位置:首页 > 其它

codeforce 492 C D

2014-12-03 22:28 274 查看
  C题贪心即可,但要用除法进行迭代,加法会超时。

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct exam{
int g,d;
}ex[100010];
bool cmp(struct exam a,struct exam b)
{
return a.d<b.d;
}
int main()
{
//freopen("in.txt","r",stdin);
int n,r;
long long now=0,tar,ave;
cin>>n>>r>>ave;
tar=ave*n;
for(int i=1;i<=n;i++) {cin>>ex[i].g>>ex[i].d;now+=ex[i].g;}
tar-=now;
sort(ex+1,ex+n+1,cmp);
int pos=1;
long long ans=0;
while(tar>0){
while(ex[pos].g==r) pos++;
if(tar>=(r-ex[pos].g)){
ans+=(long long)(r-ex[pos].g)*ex[pos].d;
tar-=r-ex[pos].g;
pos++;
}
else{
ans+=tar*ex[pos].d;
break;
}
}
cout<<ans<<endl;
return 0;
}


 

   D题我们把一秒分割为LCM(x,y)个单位,那么vanya没y个单位攻击一次,vova没x个单位攻击一次,明显先找到GCD(x,y),消去重复环节,然后暴力求解满足条件的时间,这里可以二分求解。

#include <iostream>
#include <cstdio>
using namespace std;
int gcd(int a,int b)
{
int t=1;
while(t){
t=a%b;
a=b;b=t;
}
return a;
}
int main()
{
//freopen("in.txt","r",stdin);
long long n,x,y,sum;
scanf("%I64d%I64d%I64d",&n,&x,&y);
sum=x+y;
for(int i=0;i<n;i++){
long long t;
scanf("%I64d",&t);
long long temp=gcd(x,y);
temp=x/temp*y;
sum=temp/x+temp/y;
t-=(t/sum)*sum;
long long time=0,total=0,ty=1;
long long l=0,r=temp;
while(l<r){
long long mid=(l+r)>>1;
if((mid/x+mid/y)>=t) r=mid;
else l=mid+1;
}
if(l%x==0&&l%y==0) cout<<"Both"<<endl;
else if(l%y==0) cout<<"Vanya"<<endl;
else cout<<"Vova"<<endl;
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces