牛客寒假算法基础集训营2

牛客寒假算法基础集训营2

题目链接:https://ac.nowcoder.com/acm/contest/3003

A 做游戏

两人石头剪刀布,分别给出两人石头剪刀布次数,求第一个人最多获胜局数。

尽可能让第一个人的每次出 剪刀/石头/布 对应到 第二个人出 布/剪刀/石头。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<string.h>
#include<map>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int e = 3e5 + 5;

int main()
{
    ll a, b, c, x, y, z, ans = 0;
    cin >> a >> b >> c >> x >> y >> z;
    if(a < y) ans += a;
    else ans += y;
    if(b < z) ans += b;
    else ans += z;
    if(c < x) ans += c;
    else ans += x;
    cout << ans << endl;
    return 0;
}

B 排数字

题意:给出一个字符串,将其重新排列,最多有多少个不同的子串为 616 。

思路: 遍历的1和6的个数num1,num6,以数量少的数字为准,如果1少,答案为1的数量(一个616中只有一个1);如果6少,答案为6的数量-1(一个616中有两个6)

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<string.h>
#include<map>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int e = 3e5 + 5;

int main()
{
    int n, num1 = 0, num6 = 0, ans;
    string s;
    cin >> n;
    cin >> s;
    for(int i = 0; i < n; i++){
        if(s[i] == '1') num1++;
        else if(s[i] == '6') num6++;
    }
    if(num6 > num1) ans = num1;
    else ans = num6 - 1;
    cout << ans << endl;
    return 0;
}

C 算概率

题意:n道题,第 i 道题的正确率为 pi​ ,求出这 n 题里恰好有 0,1,…,n题正确的概率分别是多少,对 109+7 取模。

对 109+7 取模的含义是:对于一个 b ≠ 0 b≠0 b=0 的不可约分数 a b a\over b ba,存在 q q q 使得 b × q   m o d   ( 1 0 9 + 7 ) = a b×q mod (10^9+7)=a b×qmod(109+7)=a q q q 即为 a b a\over b ba 对 109+7取模的结果。

将其当作正常概率计算。

思路:DP, d p [ i ] [ j ] dp[i][j] dp[i][j]表示前i道题做对j道的概率

d p [ i ] [ j ] = d p [ i − 1 ] [ j ] ∗ ( 1 − p i ) + d p [ i − 1 ] [ j − 1 ] ∗ p i dp[i][j] = dp[i - 1][j] * (1 - pi) + dp[i - 1][j - 1] * pi dp[i][j]=dp[i1][j](1pi)+dp[i1][j1]pi

d p [ i ] [ j ] dp[i][j] dp[i][j]可以是前 i − 1 i-1 i1道题对了 j j j道之后,做错了一道;也可能是前 i − 1 i-1 i1道题对了 j − 1 j-1 j1道之后,做对了一道。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int n;
ll p[2005], dp[2005][2005];//前i道题对了j道

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> p[i];
    }
    dp[0][0] = 1;
    for(int i = 1; i <= n; i++){
        dp[i][0] = dp[i - 1][0] * (1 - p[i] + mod) % mod;
        for(int j = 1; j <= i; j++){
            dp[i][j] = (dp[i - 1][j] * (1 - p[i] + mod) + dp[i - 1][j - 1] * p[i]) % mod;
        }
    }
    for(int i = 0; i <= n; i++){
        cout << dp[n][i] << " ";
    }
    return 0;
}

D 数三角

题意:平面上有n个不重合的点,这 n 个点形成的三角形中,总共有多少个钝角三角形。

思路:通过公式 a 2 > b 2 + c 2 a^2>b^2+c^2 a2>b2+c2暴力枚举。。。。注意三点可能共线的情况。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 1;
const int INF = 0x3f3f3f3f;
const int e = 3e5 + 5;

int p[505][2];
int ans = 0;

bool check(int i, int j, int k){
    int x1 = p[i][0] - p[j][0], y1 = p[i][1] - p[j][1];
    int x2 = p[i][0] - p[k][0], y2 = p[i][1] - p[k][1];
    int x3 = p[j][0] - p[k][0], y3 = p[j][1] - p[k][1];
    int d1 = x1 * x1 + y1 * y1;
    int d2 = x2 * x2 + y2 * y2;
    int d3 = x3 * x3 + y3 * y3;
    if(x1 * y2 == x2 * y1 || x2 * y3 == x3 * y2) return 0;//两点可能共线
    if(d1 > d2 + d3 || d2 > d1 + d3 || d3 > d1 + d2){//a*a>b*b+c*c
        return 1;
    }
    return 0;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p[i][0] >> p[i][1];
    }
    //枚举
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            for(int k = j + 1; k < n; k++){
                if(check(i, j, k)) ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

E 做计数

题意:求有多少个不同的正整数三元组 ( i , j , k ) (i,j,k) (i,j,k)满足 i + j = k \sqrt{i}+\sqrt{j}=\sqrt{k} i +j =k i ∗ j < = n i*j<=n ij<=n

思路:等式两侧平方,得 2 i j + i + j = k 2\sqrt{ij}+i+j=k 2ij +i+j=k,要使 k k k为整数, i j \sqrt{ij} ij 要为整数,所以 i j ij ij是一个完全平方数。

枚举1到n中的完全平方数,再求其因子数组数之和。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int main()
{
    int n, ans = 0;
    cin >> n;
    for(int i = 1; i * i <= n; i++){ //完全平方数
        for(int j = 1; j * j <= i * i; j++){//求完全平方数i*i的因子
            if((i * i) % j == 0){
                if(j * j == (i * i)) ans++;
                else ans += 2;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

F 拿物品

题意:A,B两个人选n个物品,每个物品有两个属性ai,bi。两个人轮流拿,A先拿。选取完毕后,A的得分为A拿的物品的ai之和,B的得分为B拿的物品的bi之和,两个人都希望自己的得分尽量比对方大(即最大化自己与对方得分的差),求出两人都使用最优策略的情况下,最终分别会选择哪些物品

思路:最大化自己与对方得分的差,是自己拿的多,且又让别人拿的少,所以两个属性都要考虑。将两个属性值和排序,优先选ai+bi最大的。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int e = 2e5 + 5;

typedef struct {int a, b, sum, num;} test;
test arr[e];
bool mycmp(test t1, test t2){
    if(t1.sum > t2.sum) return 1;
    else return 0;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> arr[i].a;
    }
    for(int i = 0; i < n; i++){
        cin >> arr[i].b;
        arr[i].num = i;
        arr[i].sum = arr[i].a + arr[i].b;
    }
    sort(arr, arr + n, mycmp);
    for(int i = 0; i < n; i += 2) cout << arr[i].num + 1 << " ";
    cout << endl;
    for(int i = 1; i < n; i += 2) cout << arr[i].num + 1 << " ";
    cout << endl;
    return 0;
}

G 判正误

题意:有七个整数a,b,c,d,e,f,g,验证 a d + b e + c f = g a^d+b^e+c^f=g ad+be+cf=g

思路:快速幂, 直接计算会 TLE ,需要取模
a d + b e + c f ≡ g ( m o d M ) a^d+b^e+c^f\equiv g \pmod M ad+be+cfg(modM)
代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<string.h>
#include<map>
using namespace std;

typedef long long ll;
const int mod = 1e9 + 1;
const int INF = 0x3f3f3f3f;
const int e = 3e5 + 5;

ll mypow(ll a, ll b){
    ll res = 1;
    while (b > 0){
        if (b & 1){
            res = res * a % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int main()
{
    int t;
    cin >> t;
    while(t--){
        ll a, b, c, d, e, f, g;
        cin >> a >> b >> c >> d >> e >> f >> g;
        ll res = (mypow(a, d) + mypow(b, e) + mypow(c, f)) % mod;
        g %= mod;
        if(res == g) cout << "Yes" << endl;
        else cout<< "No" << endl;
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值