Codeforces Round 913 (Div. 3) A~E

目录

[A. Rook](#A. Rook)

问题分析:

[B. YetnotherrokenKeoard](#B. YetnotherrokenKeoard)

问题分析:

[C. Removal of Unattractive Pairs](#C. Removal of Unattractive Pairs)

问题分析:

[D. Jumping Through Segments](#D. Jumping Through Segments)

问题分析:

[E. Good Triples](#E. Good Triples)

问题分析:


A. Rook

问题分析:

给一个棋子将其同行同列的位置输出

cpp 复制代码
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template<class T> bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template<class T> bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
typedef pair<int,int>pii;
const int N =1e5+7;
signed main()
{IOS
use{
    string a;cin>>a;
    char x=a[0];
    int num=a[1]-'0';
    for(int i=1;i<=8;i++){
        if(i!=num)
        cout<<x<<i<<endl;
    }
    for(int i=0;i<=7;i++){
        if('a'+i!=x){
            cout<<(char)('a'+i)<<num<<endl;
        }
    }
   
}


return 0;
}

B. YetnotherrokenKeoard

问题分析:

一个字符串,当当前字符为'b'时,删除新字符串最右边的 小写字符,当当前字符为'B'时删除新字符串最右边的大写字符,没有则不删除,除去'b'和'B'的其他字符添加到新字符串当中,然后输出。

可以通过栈来存储新字符串当中的大小写字符的位置,然后遇到相应的'b' or 'B',将栈顶的位置标记,最后按照标记输出原字符串的字符即可

cpp 复制代码
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template<class T> bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template<class T> bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
typedef pair<int,int>pii;
const int N =1e5+7;
signed main()
{IOS
use{
	string a;cin>>a;
	stack<int>lowi,upi;
	vct<bool>st(a.LEN);
	for(int i=0;i<a.LEN;i++){
		if(a[i]=='b'){
			st[i]=1;
			if(lowi.empty())continue;
			else st[lowi.top()]=1,lowi.pop();
		}
		else if(a[i]=='B'){
			st[i]=1;
			if(upi.empty())continue;
			else st[upi.top()]=1,upi.pop();
		}
		else {
			if(islower(a[i])){
				lowi.push(i);
			}else {
				upi.push(i);
			}
		}
	}
	for(int i=0;i<a.LEN;i++){
		if(!st[i])cout<<a[i];
	}cout<<"\n";
}


return 0;
}

C. Removal of Unattractive Pairs

问题分析:

给定字符串,如果相邻两个字符不同,可以删除这两个字符,求出最终可以剩下几个字符

我们统计每个字符的数量,为了保证最后剩下的字符个数最少,我们贪心地用个数次大的字符与个数最大的字符相抵消,因为需要维护数量的降序排序,我们选择使用大根堆来维护,我们每次弹出堆顶的两个元素,将他们互相抵消,也就是都减一,如果大于零就再放回堆当中,最终堆内会只剩下一个数,也就是最短的字符串长度

cpp 复制代码
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template<class T> bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template<class T> bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
typedef pair<int,int>pii;
const int N =1e5+7;
signed main()
{IOS
use{
	int n;cin>>n;
	string a;cin>>a;
	vct<int>cnt(26);
	for(int i=0;i<a.LEN;i++){
		cnt[a[i]-'a']++;
	}
	priority_queue<int>q;
	for(int i=0;i<26;i++){
	        if(cnt[i]!=0)q.push(cnt[i]);
	}
	while(q.size()>1){
		int x=q.top();
		q.pop();
		int y=q.top();
		q.pop();
		x--;
		y--;
		if(x>0)q.push(x);
		if(y>0)q.push(y);
	}
	if(q.empty())cout<<"0"<<endl;
	else cout<<q.top()<<endl;
}


return 0;
}

D. Jumping Through Segments

问题分析:

n个线段,每次可以选择走0~k格,第次一定要落在第个线段内部,要求在满足条件的情况下使得k最小.

对于k来说,k的大小对于结果的影响是单调的,故可以用二分答案,我们只需要确定第步走出的范围能够与第个线段有交集即可

cpp 复制代码
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template<class T> bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template<class T> bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
typedef pair<int,int>pii;
const int N =1e5+7;
signed main()
{
   IOS
   use{
       int n;
       cin >> n;
       int ans;
       vct<int> l(n + 1);
       vct<int> r(n + 1);
       for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
       int L = 0, R = 1e9;
       int mid;
       auto check = [&](int mid) -> bool {
           int dis1 = 0, dis2 = 0;
           for (int i = 1; i <= n; i++) {
               dis1 -= mid, dis2 += mid;
               if (dis1 > r[i] || dis2 < l[i]) return false;
               mmax(dis1, l[i]), mmin(dis2, r[i]);
           }
           return true;
       };
       while (L <= R) {
           mid = (L + R) >> 1;
           if (check(mid)) {
               R = mid - 1;
               ans = mid;
           } else L = mid + 1;
       }
       cout << ans << endl;
   }

   return 0;
}

E. Good Triples

问题分析:

给定整数n,求出有多少个三元组满足题目条件,且若三元组的元素相同但排序不同,则 算是不同的三元组。

我们通过样例以及向下推导可知:

故满足的公式,且为n各位数字得到结果的乘积

cpp 复制代码
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);
#define int long long
#define pb push_back
#define vct vector
#define checkbit __builtin_popcount
#define gcd __gcd
#define use int T;cin>>T;while(T--)
#define LEN length()
#define all(a) a.begin(),a.end()
template<class T> bool mmax(T &u, T v) { return u < v ? (u = v, 1) : 0; }
template<class T> bool mmin(T &u, T v) { return u > v ? (u = v, 1) : 0; }
#define lowbit(x) (x&(-x))
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
typedef pair<int,int>pii;
const int N =1e5+7;
signed main()
{
  IOS
  use{
     string a;cin>>a;
     int ans=1;
     for(int i=0;i<a.LEN;i++){
         ans*=(a[i]-'0'+2)*(a[i]-'0'+1)/2;
     }cout<<ans<<endl;
  }

   return 0;
}
相关推荐
柠檬少少开发18 分钟前
图像拼接算法及实现(一)
人工智能·算法·计算机视觉
weixin_486681141 小时前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言1 小时前
C++入门
开发语言·c++
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜1 小时前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
代码小狗Codog2 小时前
C++独立开发开源大数计算库 CBigNum
数据结构·c++
咕噜咕嘟嘟嘟3 小时前
343. 整数拆分
数据结构·算法
WenGyyyL3 小时前
力扣最热一百题——二叉树的直径
java·c++·算法·二叉树·深度优先
sdlkjaljafdg3 小时前
vector<bool>性能测试
开发语言·c++·算法
telllong3 小时前
使用llama.cpp 在推理MiniCPM-1.2B模型
c++·llama·llama.cpp