Codeforces Round 905 (Div. 3 A - G2)

Codeforces Round 905 (Div. 3 A - G2)

A. Morning

思路

把0看作是10 模拟

#include <bits/stdc++.h>
 

using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n;


void solve()
{
	string s;
	cin >> s;
	ll ans = 0;
	int p = 1;
	for (int i = 0; i < s.size(); i ++)
	{
		int a = s[i] - '0';
		if(!a) a = 10;
		if(a == p) ans ++;
		else 
		{
			ans += abs(a - p) + 1;
			p = a;
		}
	}

	cout << ans << endl;
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

B. Chemistry

思路

因为会重新排序,所以和子母出现位置无关,直接枚举子母出现个数,将奇数的统计出来,因为回文子串最多只能有一个奇数,所以先将子母数量减少至1个奇数,因为只有总字符串为奇数的时候才能允许出现,只需要判断第一次删除后的奇偶性即可

#include <bits/stdc++.h>
 
#define x first
#define y second
using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n,k;
string s;

void solve()
{
	cin >> n >> k >> s;
	map<char,int> mp;
	for (int i = 0; i < n; ++i) mp[s[i]] ++;
	
    int cnt = 0;
    for (auto e:mp)
    {
    	if(e.y % 2 > 0) cnt ++;
    }
    int ans = 0;
    if(cnt > 1) ans = cnt - 1;

    if((n - ans) % 2 == 0 && (cnt - ans) > 0) ans ++;
    if(ans > k) puts("NO");
    else puts("YES"); 
	
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

C Raspberries

思路

只需要有一个可以mod k = 0即可,由于k的范围非常小,且除四意外都是质数,只需要将4特判(数字奇偶性)

#include <bits/stdc++.h>
 
#define x first
#define y second
using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n,k;
int a[N];

void solve()
{
	cin >> n >> k;
    int cnt = 0;
	for (int i = 0; i < n; ++i) cin >> a[i];
    int ans = 1e9;
    for (int i = 0; i < n; i ++)
    {
        int t = k - (a[i] + k) % k;
        if((a[i] % k) == 0) t  = 0;
        ans = min(ans,t);
    }
    
    if(k == 4)
    {
         for (int i = 0; i < n ; i ++)
            if(a[i] % 2) cnt ++;
         
         if(n - cnt > 1) ans = 0;
         else if((n - cnt) == 1 && cnt >= 1) ans = min(ans, 1);
         else if(cnt >= 2) ans = min(ans, 2);
    
    }
       
     cout << ans << endl;
	 
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

反思

少敲了个等号,wa了发

D. In Love

思路

排序后

只需要判断最左面的一个右端点,和最右面的一个左端点比较

#include <bits/stdc++.h>
#include <set>
 
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n,k;
struct nodel{
    int l,r;
    friend bool operator<(const nodel a, const nodel b)
    {
        return a.l > b.l;
    }
};

struct noder{
    int l,r;
    friend bool operator<(noder a, noder b)
    {
        return a.r < b.r;
    }
};

void solve()
{
	cin >> n;
    int cnt = 0;
    multiset<nodel> ls;
    multiset<noder> rs;
    while(n --)
    {   
        char op;
        int l,r;
        cin >> op;
        cin >> l >> r;

         if (op == '+')
        {
            ls.insert({l,r});
            rs.insert({l,r});
        }
        else
        {
            ls.erase(ls.find({l,r}));
            rs.erase(rs.find({l,r}));
        }

        if((*rs.begin()).r < (*ls.begin()).l) puts("YES");
        else puts("NO");
    }
	 
}

int main()
{
    int t = 1;
    while(t --) solve();
    return 0;
}

反思

对multiset不熟练

E - Look Back

思路

如果直接暴力枚举,会超时;

只需要考虑相邻的两个需要变化几次,维护前缀和,当前这个数所要修改的次数就是前面要修改的次数和后面要修改的次数求和,当前修改的次数有可能是负的

#include <bits/stdc++.h>
 
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n;
ll a[N];



void solve()
{
	cin >> n;
    ll cnt = 0;
    for (int i = 0; i < n ; i ++) cin >> a[i];

    ll ans = 0;
    for (int i = 1; i < n ; i ++)
    {
        ll l = a[i - 1], r = a[i], ct = 0;//ct 表示是修改次数
        while(l < r) ct --, l *= 2;
        while(l > r) ct ++, r *= 2;

        cnt = max(0ll, cnt + ct);
        ans += cnt;    
    }
	 
    cout << ans << endl;
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

F - You Are So Beautiful

思路

我们考虑什么情况下两个子串会出现相等的,就是在l,r两侧有和l,r相等的元素那么这个子串会出现重复的,所以要记录每个元素第一次和最后一次出现的位置,扫描数组如果是最后一次出现那么前面所有的第一次出现的数字都可以使用,维护前缀第一次出现子母的个数

#include <bits/stdc++.h>
 
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n,k;
int  a[N];



void solve()
{
    cin >> n;
    for (int i = 0 ;i <n ; i ++) cin >> a[i];
    
    ll ans = 0;
    map<int,int > first, end,cnt;

    for (int i = 0; i < n ; i ++)
    {
        if(cnt[a[i]] == 0)
        {
            cnt[a[i]] ++;
            first[a[i]] = i;
            end[a[i]] = i;
        }
        else
        {
             end[a[i]] = i;
        }
    }

    int sum = 0;
    
    for (int i = 0; i < n ; i ++)
    {
        if(first[a[i]] == i) sum ++;

        if(end[a[i]] == i) ans += sum;
    }
    

    cout << ans << endl;
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

G1. Dances (Easy version)

思路

贪心加二分,因为m只等于1

将两个排完序,a[0] = 1;

直接二分查找符合条件的最小值

如何维护删除操作,就是将b数列中最小的n - mid个删除

#include <bits/stdc++.h>
 
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n,m;


int a[N], b[N];

bool check(int x)
{
    int k = n - x;
    for (int i = 0; i < x ; i ++)
        if(a[i] >= b[i + k])
            return false;
    
    return true;
}

void solve()
{
	cin >> n >> m;
    
    for (int i = 1; i < n ; i ++) cin >> a[i];
    a[0] = 1;
    for (int i = 0; i < n ; i ++) cin >> b[i];

    sort(a, a + n);
    sort(b, b + n);    
    
    int l = 0,r  = n;
    while(l < r)
    {
        int mid = l + r + 1 >> 1;
        if(check(mid)) l = mid;
        else r = mid - 1;
    }

    cout << n - l <<endl;
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

G2 - Dances (Hard Version)

https://codeforces.com/contest/1883/problem/G2

思路

请教过我校大佬后明白的题

贪心加双指针

首先因为可以重新排序,所以我们先不考虑a序列中的第一个元素,直接考虑题目输入的数组,然后遍历a数组的同时找到第一

比a数组当前数字大的数做一个标记,知道有一个数组被遍历完

分为两种情况

1.a数组被遍历完,表示不用删元素就可以达到题目要求

2.b数组被遍历完,这时候已经给定的a数组剩余的都需要删除

然后我们考虑第一个元素,这时候我们要从b数组中找到最大的元素与其比较(bmax)

1.首先考虑那些元素无序删除,那么就是所有比bmax小的都不需要删除,删除的元素就是c0*第一步删除的元素

2.在考虑那些元素被删除,那么就是所有被>=bmax都需要删除,删除的元素是(bmax * (cnt + 1))

#include <bits/stdc++.h>
 
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;
const double eps = 1e-8;
 
int n,m;
int a[N], b[N],c[N];

void solve()
{
	cin >> n >> m;
    
    for (int i = 0; i < n - 1; i ++) cin >> a[i],c[i] = a[i];
    for (int i = 0; i < n ; i ++) cin >> b[i];

    
    sort(b, b + n); 
    sort(a, a + n - 1);

    vector<int> st(n + 10);
    int i = 0, j = 0;
    for (; i < n - 1 && j < n; i ++, j ++)
    {
        while(j < n && a[i] >= b[j]) j++;
        if(j >= n) break;
        if(a[i] < b[j])
        {
            st[j] = 1;
        }
    }
    int cnt = n - 1 - i;
    int c0 = 0, c1 = 0;
    for (int i = n - 1; i >= 0 ; i --)
    {
        if(!st[i])
        {
            c0 = min(m, b[i] - 1);//计算可以保留a[1]的个数
            c1 = max(0, m - b[i] + 1);//计算需要删除a[1]的m的个数
            break;
        }
    }
    ll ans = 1ll * c0 *cnt + 1ll * c1 * (cnt + 1);
    cout << ans <<endl;
}

int main()
{
    int t;cin >> t;
    while(t --) solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[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: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[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: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值