Codeforces Round 964 (Div. 4)

传送门:https://codeforces.com/contest/1999

A. : A+B Again?

题意:给定一个两位数,求位数之和

模拟:

代码:

#include<bits/stdc++.h>
using namespace std;
void solve()
{
    long long sum = 0;
    string s;cin >> s;
    for( auto e : s )sum += (e - '0');
    cout << sum << endl;
}
int main()
{
    int tt;cin>> tt;
    while(tt--)solve();
    return 0;
}

B: Card Game

题意:

思路:模拟

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int f( int a , int  b)
{
    if( a > b )return 1;
    else if( a == b )return 0;
    else return -1;
}
void solve()
{
    int x1 , x2 , y1 , y2 ;
    cin >>x1 >> x2 >> y1 >> y2;
    int sum = 0;
    if( f(x1 , y1 ) + f(x2 , y2 ) > 0 )sum++;
    if( f(x1 , y2) + f(x2 , y1 ) > 0 )sum++;
    if( f(x2 , y1) + f(x1 , y2 ) > 0 )sum++;
    if( f(x2,y2) + f(x1, y1) >0 )sum++;
    cout << sum << endl;
}
signed main()
{
	int tt; cin >> tt;
	while (tt--)solve();
	return 0;
}

C:Showering

思路:贪心 + 模拟
代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> iint;
void solve()
{
	int n, s, m;
	cin >> n >> s >> m;
	vector<iint> res;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		res.push_back({ a,b });
	}
    sort(res.begin(),res.end());
    res.push_back({ m,0 });
    int pre = 0;
    for (int i = 0; i < res.size(); i++)
    {
        if (res[i].first - pre >= s) {
            cout << "YES" << endl; return;
        }
        else pre = res[i].second;
    }
    cout << "NO" << endl;
}
int main()
{
	int tt;
	cin >> tt;
	while (tt--)solve();
	return 0;
}

D: Slavic's Exam

思路: 双指针

代码:

#include<bits/stdc++.h>
using namespace std;
void solve()
{
    string s;
    string t;
    cin >> s >> t; int n1 = s.size(); int n2 = t.size();
    s = " " + s; t = " " + t;
    int left = 1;
    int i = 0;
    for (i = 1; i <= n1 && left <= n2; i++)
    {
        if (s[i] == t[left])
        {
            left++;
        }
        else if (s[i] == '?')
        {
            s[i] = t[left]; left++;
        }
    }
    for (; i <= n1; i++)
        if (s[i] == '?')s[i] = 'a';
    if (left <= n2)cout << "NO" << endl;
    else {
        string k;
        for (int i = 1; i < s.size(); i++)
        {
            k.push_back(s[i]);
        }
        cout << "YES" << endl << k << endl;
    }
}
int main()
{
    int tt;
    cin >> tt;
    while (tt--)solve();
    return 0;
}

E: Triple Operations

题意:

思路:

1. 若要将所有数字变为0,则只能操作[ y/3] ,则要多进行操作[ y/3 ] ,另一个数字要进行 x * 3 , 则x = 0

2. 什么数字最容易变为0 , 最小的数字,先让最小的数字变为0,然后让其他的数字变为0

3. 每个数字变为0的操作次数是不变的,则可以用前缀和

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int f[N]; int a[N];
int test( int num )
{
    int count = 0;
    while(num)
    {
        count++; num /= 3;
    }
    return count;
}
void solve()
{
    int l , r;cin>>l>>r;
    cout << f[r] - f[l-1] + a[l] << endl;
}
int main()
{
    int tt;
    cin >> tt;
    f[0] = 0;
    for( int i = 1; i <= 2e5;i++)
    {
        a[i] = test(i);
        f[i] = f[i-1] + a[i];
    }
    while(tt--)solve();
    return 0;
}

F: Expected Median

题意:

思路:

1. 子序列中 1 的个数 多于 0 的个数

2. 数组中0 1没有区别(选出来之后还要排序)

3. 若1的个数为 i ,则0的个数为 k - i ,有乘法原理得C( i , cnt1 ) * C( k - i, cnt0 )

补充:求组合数常见的方式:

1.递推
void test()
{
    for( int i = 0; i < N; i++)
        for( int j = 0 ; j <= i;j++)
        {
            if( !j ) f[i][j] = 1;
            else f[i][j] = f[i-1][j] + f[i-1][j-1];
        }
}
2.乘法逆元
int qmi( int a , int b , int p )
{
    int res = 1;
    while( b )
    {
        if( b & 1 ) res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
}
int main()
{
    fact[0] = infact[0] = 1;
    for( int i = 1; i < N;i++)
    {
        fact[i] = fact[i] * i % mod;
        infact[i] = infact[i-1] * qmi( i , mod - 2 , mod ) % mod;
    }
    求C(a,b)
    int temp = fact[a] * infact[b] % mod * infact[a-b] % mod;
}

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int a[N];
const int mod = 1e9 + 7;
typedef long long LL;
int fact[N]; int infact[N];
int qmi( int a ,int k ,int p )
{
    int res = 1;
    while(k)
    {
        if(k&1) res = res * a % p;
        a = a * a % p;
        k >>= 1;
    }
    return res;
}

void solve()
{
    int n, k; cin >> n >> k;
   
    int count_0 = 0, count_1 = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] == 1)count_1++;
        else count_0++;
    }
    int res = 0;
    for (int i = k / 2 + 1; i <= k; i++)
    {
        if (i <= count_1 && k - i <= count_0)
        {
            res = ( res + fact[count_1] * infact[i] % mod * infact[count_1 - i] % mod * fact[count_0] % mod * infact[k-i] % mod * infact[count_0 - k + i] % mod ) % mod;
        }
    }
    cout << res << endl;
}
signed main()
{
    int tt;
    cin >> tt;
    fact[0] = infact[0] = 1;
    for( int i = 1; i < N;i++)
    {
        fact[i] = fact[i-1] * i % mod;
        infact[i] = infact[i-1] * qmi(i , mod -2 , mod ) % mod;
    }
    while (tt--)solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值