9.5训练日志

来源:互联网 发布:workbench网络培训平台 编辑:程序博客网 时间:2024/05/29 17:26

Kirill And The Game

Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each potion is characterized by two integers — amount of experience and cost. The efficiency of a potion is the ratio of the amount of experience to the cost. Efficiency may be a non-integer number.

For each two integer numbers a and b such that l ≤ a ≤ r and x ≤ b ≤ y there is a potion with experience a and cost b in the store (that is, there are (r - l + 1)·(y - x + 1) potions).

Kirill wants to buy a potion which has efficiency k. Will he be able to do this?
水题,水水过,直接代码.

#include<bits/stdc++.h>using namespace std;int main() {    int l,r,x,y,i;    double k;    cin>>l>>r>>x>>y>>k;    for(i=x; i<=y; i++)        if(k*i==(int)k*i&&k*i>=l&&k*i<=r) {            cout<<"YES";                return 0;        }    cout<<"NO";    return 0;}

Gleb And Pizza

Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.

The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i -th piece of the sausage is ri, and the center is given as a pair (xi, yi).

Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.
距离公式算圆心距离与两圆半径和/差即可,也较水.

#include<bits/stdc++.h>using namespace std;int main() {    int r,d,m,x,y,r1,i,ans=0;    double l;    cin>>d>>r>>m;    r=d-r;    for(i=1; i<=m; i++) {        cin>>x>>y>>r1;        l=sqrt(x*x+y*y);        if(l+r1<=d&&l-r1>=r)ans++;        }    cout<<ans;    return 0;}

Mike and gcd problem

Mike has a sequence A = [a1, a2, …, an] of length n. He considers the sequence B = [b1, b2, …, bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .

Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it’s possible, or tell him that it is impossible to do so.

is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).
比较坑,如果不是一遍yes并且不全都是0,只要判2即可,坑题

#include<bits/stdc++.h>using namespace std;int a[1000005];int main() {    int n,i,maxn=-999,x,ans=0,m=0,l=0;    cin>>n;    for(i=1; i<=n; i++) {        scanf("%d",&a[i]);        maxn=max(maxn,a[i]);        if(a[i]!=0)m=1;        if(a[i]==1)l=1;        if(i==n&&m==0) {            cout<<"NO";            return 0;        }    }    if(maxn>=2&&l!=1)    for(i=2; i<=maxn; i++) {        for(x=1; x<=n; x++) {            if(a[x]%i!=0)break;            if(x==n) {                cout<<"YES"<<endl<<0;                return 0;            }        }    }    for(x=1; x<=n; x++) {        if(a[x]%2!=0)            while(a[x]%2!=0) {                int j=a[x];                ans++;                a[x]=a[x]-a[x+1];                a[x+1]=j+a[x+1];            }    }    cout<<"YES"<<endl<<ans;    return 0;}

Bank Hacking

较难,比赛时来不及看,主要是推出每个银行最多加一或加二,一个可不加,分类判断出答案是最大数/最大数+1/最大数加二即可.

#include<bits/stdc++.h>using namespace std;const int mod=1e9+10;vector <int> a[1000000];int b[10000000],start,end;int main() {    int n,m=-mod,x;    int k1=0,k2=0;    scanf("%d",&n);    for(int i=1; i<=n; i++) {        scanf("%d",&b[i]);        m=max(m,b[i]);    }    for(int i=1; i<=n-1; i++) {        scanf("%d %d",&start,&end);        a[start].push_back(end);        a[end].push_back(start);    }    for(int i=1; i<=n; i++) {        if(b[i]==m) {            k1++;            x=i;        } else if(b[i]==m-1) k2++;    }    if(k1==1) {        int ans=0;        for(int i=0; i<a[x].size(); i++) {            if(b[a[x][i]]==m-1) ans++;        }        if(ans==k2) printf("%d",m);        else printf("%d",m+1);    } else {        bool flag=false;        for(int i=1; i<=n; i++) {            int ans=0;            if(b[i]==m) ans++;            for(int p=0; p<a[i].size(); p++) {                if(b[a[i][p]]==m) ans++;            }            if(ans==k1) flag=true;        }        if(flag) printf("%d",m+1);        else printf("%d",m+2);    }    return 0;}

总结

还要训练思维,不然遇到坑题很麻烦.

原创粉丝点击