2020牛客寒假算法集训营2

A.做游戏

https://ac.nowcoder.com/acm/contest/3003/A

签到题:只要求两个人当中最小的值就行了.

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int a,b,c,x,y,z;
int ans=0;
int main() {
  ios::sync_with_stdio(false);
  cin>>a>>b>>c>>x>>y>>z;
  ans=min(a,y)+min(b,z)+min(c,x);
  cout<<ans<<endl;
    return 0;
}

B.排数字

https://ac.nowcoder.com/acm/contest/3003/B

将字符串遍历一遍,然后记录'1'和'6'的个数

1.'1'的个数>='6'的个数,ans='6'的个数-1;

2.'1'的个数<'6'的个数,ans='1'的个数;

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int n;
string s;
ll num[N];
ll ans=0;
int main() {
  ios::sync_with_stdio(false);
  cin>>n>>s;
  memset(num,0,2e5+10);
  for(int i=0;i<n;i++){
      if(s[i]=='1')   num[1]++;
      if(s[i]=='6')   num[6]++;
  }
   if(num[6]<=num[1])  ans=num[6]-1;
   if(num[6]>num[1])   ans=num[1];
   if(num[6]<2 || num[1]==0) ans=0;
  cout<<ans<<endl;
    return 0;
}

D.数三角

https://ac.nowcoder.com/acm/contest/3003/D

额,这几主要考思维和几何,首先我们用结构体记录每个点的x和y,然后暴力遍历三个点(注意不能有重复),判断这三个点是否构成三角形,如果可以再次判断三条边长看是不是钝角三角形

注意!一定要判断三个点是否在同一直线上!!!

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int n;
struct S{
    long double x;
    long double y;
}p[510];
int ans=0;
int main() {
  ios::sync_with_stdio(false);
  cin>>n;
  for(int i=1;i<=n;i++)
      cin>>p[i].x>>p[i].y;
  for(int i=1;i<=n-2;i++)
      for(int j=i+1;j<=n-1;j++)
          for(int k=j+1;k<=n;k++) {
              long double a = (p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y);
              long double b = (p[i].x - p[k].x) * (p[i].x - p[k].x) + (p[i].y - p[k].y) * (p[i].y - p[k].y);
              long double c = (p[j].x - p[k].x) * (p[j].x - p[k].x) + (p[j].y - p[k].y) * (p[j].y - p[k].y);
              a=sqrt(a),b=sqrt(b),c=sqrt(c);
              if (a+b>c && a+c>b && b+c > a && p[i].x/p[j].x!=p[j].x/p[k].x && p[i].y/p[j].y!=p[j].y/p[k].y) {
                  if (a*a>c*c+b*b || c*c>a*a+b*b || b*b>a*a+c*c)
                      ans++;
              }
          }
  cout<<ans;
    return 0;
}

E.做计数

https://ac.nowcoder.com/acm/contest/3003/E

将等式两边平方得:i+j+sqrt(i*j)=k,所以只要i*j是完全平方数,即可满足条件,暴力枚举统计答案即可;

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int n;
int ans=0;
int main() {
  ios::sync_with_stdio(false);
  cin>>n;
  for(int i=1;i<=sqrt(n);i++){
      int k=i*i;
      for(int j=1;j<=i;j++){
          if(i==j)  ans++;
          else if(k%j==0)  ans+=2;
      }
  }
  cout<<ans<<endl;
    return 0;
}

F.拿物品

https://ac.nowcoder.com/acm/contest/3003/F

这题我WA了十发,最开始我以为只要用一个结构体数组记录每个物品的a,b值然后交叉排序a,b的最大值就行了,但我忘了一点:可以选择抢走对方的值大的物品;

然后我想了一下,(就当我是第一个人)假如这个物品a大b也大,那我拿走这个肯定是赚的,假如a小b也小,那我肯定不拿这个,转念一想a大b小和a小b大应该算是同一种情况,于是我们将a和b相加,从大到小进行排序;

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int n;
struct S{
    ll a;
    ll b;
    ll num;
    ll t;
}p[200010];
bool cmp(S x,S y){
    return x.t>=y.t;
}

int main() {
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p[i].a;
        p[i].num=i;}
    for(int i=1;i<=n;i++)
        cin>>p[i].b;
   for(int i=1;i<=n;i++){
    p[i].t=p[i].a+p[i].b;
   }
   sort(p+1,p+1+n,cmp);
   for(int i=1;i<=n;i++){
    if(i%2==1)
    cout<<p[i].num<<" ";
   }
   cout<<endl;
   for(int i=1;i<=n;i++){
       if(i%2==0)
    cout<<p[i].num<<" ";
   }
    return 0;
}

G.判正误

https://ac.nowcoder.com/acm/contest/3003/G

快速幂,但是直接算的话必然爆掉,模一个1e9+7即可;

这里讲一下快速幂:利用递归将指数分解,如果指数为偶,将其二分相乘

若为奇,将其减1和自己相乘;

eg:2^10=2^5*2*5 ..........(递归)

    2^11=2*2^10 ..........(递归)

#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
int t;
ll a,b,c,e,f,g,d;
ll mod=1e9+7;
ll pow(ll a,ll b,ll m){
    if(b==0)  return 1;
    else if(b%2==1)  return a*pow(a,b-1,m)%m;
    else{
        ll num=pow(a,b/2,m)%m;
        return num*num%m;
    }
}
int main() {
  ios::sync_with_stdio(false);
  cin>>t;
  while(t--){
      cin>>a>>b>>c>>e>>f>>g>>d;
      ll ans1=pow(a,e,mod);
      ll ans2=pow(b,f,mod);
      ll ans3=pow(c,g,mod);
      ll ans=ans1+ans2+ans3;
      if(ans==d) cout<<"Yes"<<endl;
      else   cout<<"No"<<endl;
  }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lr599909928/p/12273982.html