Codeforces Round 964 (Div. 4)全部题解

A. A+B Again?

题目大意

给定n求数位和。

思路

代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
};
void solve() {
    ll n; cin >> n;
    ll ans = 0;
    while (n) {
        ans += n % 10;
        n /= 10;
    }
    cout << ans << "\n";
}
int main() {
    //fastread();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

B. Card Game

题目大意

Su,Sl两个人做游戏,问Su胜利的所有局内,胜利的回合数。

注意,比赛顺序不同视为不同答案。

思路

注意题意即可。

代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
};
void solve() {
    ll a, b, c, d; cin >> a >> b >> c >> d;
    ll ans = 0;
    if (a > c && b > d)ans+=2;
    if (a > d && b > c)ans+=2;
    if (a > c && b == d)ans += 2;
    if (a > d && b == c)ans += 2;
    if (a == c && b > d)ans += 2;
    if (a == d && b > c)ans += 2;
    cout << ans << "\n";
}
int main() {
    //fastread();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

C. Showering

题目大意

给定一个洗澡时间和n个任务时间段,问是否有时间洗澡。

思路

按照题意模拟即可,注意开头一段和最后一段。

代码实现
​
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
};
void solve() {
    ll n, s, m; cin >> n >> s >> m;
    ll be = 0;
    bool f = false;
    for (int i = 0; i < n; i++) {
        ll l, r; cin >> l >> r;
        if (l - be >= s) {
            f = true;
        }
        be = r;
        if (i == n - 1) {
            if (m - be >= s)f = true;
        }
    }
    if (f)cout << "YES\n";
    else cout << "NO\n";
}
int main() {
    //fastread();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

​

D. Slavic's Exam

题目大意

给定一个包含‘?’的字符串s,问是否有子序列字符串t。

思路

注意到是子序列,贪心即可,能匹配的尽量匹配。

代码实现
​
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
};
void solve() {
    string s, t; cin >> s >> t;
    ll l = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == t[l] || s[i] == '?') {
            s[i] = t[l];
            l++;
        }
        if (l == t.size())break;
    }
    if (l == t.size()) {
        cout << "YES\n";
        for (auto c : s) {
            if (c == '?')cout << "a";
            else cout << c;
        }
        cout << "\n";
    }
    else {
        cout << "No\n";
    }
}
int main() {
    //fastread();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

​

E. Triple Operations

题目大意

在黑板上写下l,r区间内所有数。给定操作,每次选择两个数,让一个数除三,一个数乘三问什么时候。问多少次操作后所有数均为0。

思路

每次先处理出来一个0,然后再处理其他数,处理出0的过程就是ans+2。

预处理处出来所有数被三除成0的次数,然后做个前缀和。每次访问访问前缀和即可。

代码实现
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
};
ll dp[210000];
void dfs() {
    dp[0] = 1;
    ll cnt = 1;
    for (int i = 3; i <= 2e5; i *= 3) {
        dp[i] = ++cnt;
    }
    cnt = 1;
    for (int i = 1; i <= 2e5; i++) {
        if (dp[i] == 0) {
            dp[i] = cnt;
        }
        else {
            cnt++;
        }
        dp[i]+=dp[i-1];
    }
}
void solve() {
    ll l, r; cin >> l >> r;
    ll ans = 0;
    ans+=dp[r]-dp[l];
    ans += (dp[l]-dp[l-1]) * 2;
    cout << ans << "\n";
}
int main() {
    //fastread();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    dfs();
    while (t--) {
        solve();
    }
    return 0;
}

F. Expected Median

题目大意

求序列n的所有长度奇数k的子序列的中位值和,序列n只包含01。

思路

找出0、1个数。在子序列中,1的个数大于一半的时候,答案有效。

枚举,然后乘法逆元求组合数即可。

代码实现
#include<iostream>
#include<algorithm>
#include<array>
#include<string>
#include<math.h>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<cstdio>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
#define endl '\n'
#define ll long long
#define ld long double
#define ull unsigned long long
#define N 1000000
#define yes "YES"
#define no "NO"
#define pairl pair<ll,ll>
#define debug cout << "debug:" << endl;
const double pi = acosl(-1);
using namespace std;
//快速幂取模
ll qpow(ll a, ll n, ll p)
{
    ll ans = 1;
    while (n)
    {
        if (n & 1) ans = ans * a % p;
        a = a * a % p;
        n >>= 1;
    }
    return ans;
}
//阶乘
vector<ll>jie(N);
void jc(ll n, ll mod) {
    jie[0] = 1;
    for (int i = 1; i <= n; i++) {
        jie[i] = i * jie[i - 1];
        jie[i] %= mod;
    }
}
//阶乘逆元
vector<ll>njie(N);
void njc(ll n, ll mod) {
    njie[n] = qpow(jie[n], mod - 2, mod);
    for (int i = n; i >= 1; i--) {
        njie[i - 1] = njie[i] * i % mod;
    }
}
void solve() {
    ll n, k;
    cin >> n >> k;
    ll mod = 1e9 + 7;
    vector<ll>a(n);
    ll b[2] = { 0 };
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b[a[i]]++;
    }
    ll ans = 0;
    for (int i = max(0ll, k - b[0]); i <= min(k, b[1]); i++) {
        if (i <= k / 2)continue;
        ll x = (jie[b[0]] * njie[k - i]) % mod * njie[b[0] - k + i] % mod;
        ll y = (jie[b[1]] * njie[i]) % mod * njie[b[1] - i] % mod;
        ans += x * y % mod;
        ans %= mod;
    }
    cout << ans << endl;
​
}
int main() {
    ll mod = 1e9 + 7;
    jc(2e5 + 10, mod);
    njc(2e5 + 10, mod);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

G1,2. Ruler (easy version)

题目大意

给个尺子,尺子再2-999缺了个数,要求在n次内询问到x的值。

G1中n为10,G2中n为7。

思路

G1二分求,G2三分求。

没啥好说的,这里只给出G2代码,也能过G1,注意代码细节即可。

代码实现
#include<iostream>
#include<algorithm>
#include<array>
#include<string>
#include<math.h>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<cstdio>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define Mod 998244353
#define PI acos(-1);
#define MAXN 210000
#define debug cout<<"debug:\t"
#define debugn cout<<"debug:\n"
#define enter cout<<"\n"
void fastread() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
};
void solve() {
    ll bel = 1;
    ll ber = 999;
    ll l = (ber - bel) / 3 + bel;
    ll r = (ber - bel) / 3 * 2 + bel;
    ll sum = 0;
    while (bel < ber) {
        cout << "? " << l << " " << r << endl;
        cin >> sum;
        if (sum == (l + 1) * (r + 1)) {
            ber = l;
            l = (ber - bel) / 3 + bel;
            r = (ber - bel) / 3 * 2 + bel;
        }
        else if (sum == l * (r + 1)) {
            bel = l;
            ber = r;
            l = (ber - bel) / 3 + bel;
            r = (ber - bel) / 3 * 2 + bel;
        }
        else if (sum == l * r) {
            bel = r + 1;
            l = (ber - bel) / 3 + bel;
            r = (ber - bel) / 3 * 2 + bel;
        }
    }
    cout << "! " << bel << endl;
}
int main() {
    //fastread();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 887是一个程序设计竞赛活动,由Codeforces组织举办。根据引用中的代码,该竞赛的题目要求解决一个序列操作的问题。给定一个长度为n的序列,通过执行一系列操作,使得序列变得非sorted,即非严格递增。具体操作是将序列中[1, i]范围内的数字全部加一,同时将[i+1, n]范围内的数字全部减一。问题要求求解最少需要执行多少次操作才能达到要求。 引用中的代码给出了解决这个问题的实现。代码首先读入序列的长度n和序列a。然后通过判断序列是否已经是非sorted,如果是则直接输出0。接下来,代码遍历序列,求出相邻两个数字的差的最小值。最后,计算出最少需要执行的操作次数,并输出结果。 需要注意的是,引用中的代码只是给出了解决问题的一种实现方式,并不代表Codeforces Round 887的具体题目和解答。要了解该竞赛的具体信息,需要参考Codeforces官方网站或相关资料。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Codeforces Round 887 (Div. 2)](https://blog.csdn.net/qq_36545889/article/details/131905067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值