Codeforces Round 918 (Div. 4)(AK)

本文详细解读了四道Codeforces竞赛题目,包括模拟解题方法(A、B)、利用前缀和和二分查找优化(C、E),以及自然语言处理(D)的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A、模拟

B、模拟

C、模拟

D、模拟

E、思维,前缀和

F、思维、逆序对

G、最短路

A - Odd One Out 

    题意:给定三个数字,有两个相同,输出那个不同的数字。

    直接傻瓜写法

void solve() 
{
	int a , b , c;
	cin >> a >> b >> c;
	if(a == b){
		cout << c << endl;
	}	
	else if(a == c){
		cout << b << endl;
	}
	else
		cout << a << endl;
}    

 B - Not Quite Latin Square

        题意:给定一个3*3的矩阵,每一行每一列都有且仅有A、B、C三个字母组成。现在给出矩阵中有一个? , 求这个?代表哪个字母。

        可以用二进制表示三个字母是否存在

void solve() 
{
	string s[3];
	for(int i = 0 ; i < 3; i ++)
		cin >> s[i];
	for(int i = 0 ; i < 3 ;i ++){
		int mask = 0;
		for(int j = 0 ; j < 3 ; j ++){
			mask += (1 << (s[i][j] - 'A')) * (s[i][j] != '?');
		}
		if(mask != 7){
			for(int j = 0 ; j < 3 ; j ++){
				if((mask >> j) & 1){
					continue; 
				}
				else{
					char c = j + 'A';
					cout << c <<endl;
				}
			}
		}
	}
}     

C - Can I Square? 

        题意:给定一个数组,求数组之和能否形成完全平方数。

        注意:直接用sqrt会因为精度问题而出错,所以在[sqrt - 2 , sqrt + 2]之间都试一遍即可。

        

void solve() 
{
	LL sum = 0;
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
		int x;
		cin >> x;
		sum += x;
	}	
	LL t = sqrt(sum);
	for(LL i = t - 1 ; i <= t + 1 ; i ++){
		if(i < 0)
			continue;
		if(i * i == sum){
			cout <<"YES\n";
			return;
		}
	}
	cout <<"NO\n";
}    

二分写法:

void solve() 
{
	LL sum = 0;
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
		int x;
		cin >> x;
		sum += x;
	}	
	auto check = [&](LL x){
		return (x * x >= sum);
	};
	LL l = 1 , r = 1e9;
	while(l < r){
		int mid = (l + r) / 2;
		if(check(mid)){
			r = mid;
		}
		else{
			l = mid + 1;
		}
	}
	if(l * l == sum)
		cout <<"YES\n";
	else
		cout <<"NO\n";
}  

 D - Unnatural Language Processing 

        题意:

        思路:将a、e看成0,b、c、d看成1。整个单词变成了一个01串,然后发现:当连续的两个1出现时,前一个1需要放到前面的音节结尾。当只有一个连续的1,那么这个1就是音节的开头。然后模拟整个过程就行。

        

// Problem: D. Unnatural Language Processing
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
//10 101
void solve() 
{
	cin >> n;
	string s;
	cin >> s;
	for(int i = 0 ; i < n ; i ++){
		if(s[i] == 'a' || s[i] == 'e'){
			a[i] = 0;
		}
		else{
			a[i] = 1;
		}
	}
	for(int i = 0 ; i < n ; i ++){
		if(a[i] == 1){
			cout << s[i];
		}
		else if(a[i] == 0){
			if(i < n - 3){
				if(a[i + 1] == 1 && a[i + 2] == 1){
					cout << s[i] << s[i + 1] <<"."; 
					i++;
				}
				else{
					cout << s[i] <<".";
				}
			}
			else if(i == n - 3){
				cout << s[i] <<".";
			}
			else if(i == n - 2){
				cout << s[i] << s[i + 1];
				i++;
			}
			else{
				cout << s[i];
			}
		}
	}
	cout << endl;
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

E - Romantic Glasses 

        题意:给定一数组,求其中是否存在某个连续子序列是否满足\sum _{i = l}^{r}a[i](i\ mod\ 2==0) = \sum _{i = l}^{r}a[i](i\ mod\ 2 == 1)

        思路:转移之后有公式\sum _{i = l}^{r}a[i](i\ mod\ 2==0) + \sum _{i = l}^{r}-a[i](i\ mod\ 2 == 1) = 0,也就是对于原数组的奇数项都乘 -1 之后,求是否存在某个区间之和为0。

        用前缀和sum数组来表示区间,也就是存在sum(r) - sum(l) = 0 , 也就是sum(r) = sum(l)。因此我们可以逐步递增 r,然后看之前是否出现过sum(l)sum(r)相等。可用map或者set来存之前出现过的前缀和情况。这样整个复杂度为O(NlogN)

        

// Problem: E. Romantic Glasses
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
#define int long long
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
void solve() 
{
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
		cin >> a[i];
		if(i % 2 == 1){
			a[i] *= -1;
		}
	}	
	set<int>pre;
	pre.insert(0);
	int sum = 0;
	for(int i = 0 ; i < n ; i ++){
		sum += a[i];
		//cout << sum << endl;
		if(pre.count(sum)){
			cout <<"YES\n";
			return;
		}
		pre.insert(sum);
	}
	cout <<"NO\n";
	return;
}            
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

 F - Greetings 

        题意:

        思路:将所有人的起点按照从小到大进行排序,这样就满足了后面的人不会撞到前面的人(只存在后面的人已经到达终点了,然后被前面的人撞)。然后再考虑能够撞多少个人:对于排完序以后的第i个人而言,他能撞到的人是i以后的,终点小于等于b_{i}的人。因此也就是(b_{i} \geq b_{j})(i < j)的个数,也就是按照起点排完序之后的b数组的逆序对数量,然后套一遍逆序对的板子即可。

// Problem: F. Greetings
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/F
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N = 5e05+10;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
int tmp[N];
LL merge_sort(int q[], int l, int r)
{
    if (l >= r) return 0;
    
    int mid = (l + r) >> 1; // 二分区间
    
    LL res = merge_sort(q, l, mid) + merge_sort(q, mid + 1, r);
    //归并
    int i = l, j = mid + 1, k = 0;
    
    while (i <= mid && j <= r)
    {
        if (q[i] <= q[j]) tmp[k ++] = q[i ++]; // 前面的排序正常,注意`=` 说明不是逆序对
        else
        {
            res += mid - i + 1;
            tmp[k ++] = q[j ++];
        }
    }
    // 扫尾工作
    while (i <= mid) tmp[k ++] = q[i ++];
    while (j <= r) tmp[k ++] = q[j ++];
    
    for (int i = l, j = 0; i <= r; i ++ , j ++) q[i] = tmp[j];
    
    return res;
}
void solve() 
{
	cin >> n;
	pair<int,int>po[n];
	for(int i = 0 ; i < n ; i++){
		cin >> po[i].x >> po[i].y;
	}	
	sort(po , po + n);
	int a[n];
	for(int i = 0 ; i < n ; i ++){
		a[i] = po[i].y;
	}
	LL ans = merge_sort(a , 0 , n  - 1);
	cout << ans <<endl;
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

 G - Bicycles 

        题意:

       

        思路:观察到数据不大。因此直接考虑最短路算法。需要注意的是,整个过程不仅仅有点这一个条件,还有自行车速度系数这个限制。因此需要将这两个限制都表示出来,具体看代码注释

// Problem: G. Bicycles
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/contest/1915/problem/G
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
#define int long long
const LL maxn = 4e05+7;
const LL N = 1010;
const LL mod = 1e09+7;
const int inf = 0x3f3f3f3f;
const LL llinf = 5e18;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >mi;//小根堆
priority_queue<LL> ma;//大根堆
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
vector<int>a(N , 0);
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
struct node{
	int num;
	int dis;
	bool operator > (const node &t) const
	{
		return dis > t.dis;
	}
	int own;
}tmp;
int dis[N][N];//到达i点,且拥有自行车系数j的最短距离
int vis[N][N];//到达i点,且拥有自行车系数j的可能性
vector<node>tr[N];
int cost[N];
void dij(int s)
{
	for(int i = 1 ; i <= n ; i ++){
		for(int j = 0 ; j <= 1000 ; j ++){
			dis[i][j] = llinf;
		}
	}
	priority_queue<node,vector<node> , greater<node> > q;
	q.push({s , 0 , cost[1]});
	dis[s][cost[1]] = 0;
	while(!q.empty())
	{
		tmp = q.top();
		int x = tmp.num;//所在地
		int y = tmp.own;//拥有的自行车系数
		q.pop();
		if(vis[x][y] == 1)
			continue;
		vis[x][y] = 1;
		for(int i = 0 ; i < (int)tr[x].size() ; i ++ )
		{
			node now = tr[x][i];
			int len = tr[x][i].dis;//距离
			int e = tr[x][i].num;//目标地
			int pp = min(y , cost[e]);//到达目的地之后所拥有的自行车系数
			if(dis[e][pp] > dis[x][y] + len * y)
			{
				dis[e][pp] = dis[x][y] + len * y;
				q.push({e , dis[e][pp] , pp});
			}
		}
	}
}
void solve() 
{
	cin >> n >> m;
	for(int i = 1 ; i <= n ; i ++){
		tr[i].clear();
		for(int j = 0 ; j <= 1000 ; j ++){
			vis[i][j] = 0;
		}
	}
	for(int i = 0 ; i < m ; i ++){
		int u , v , dis;
		cin >> u >> v >> dis;
		tr[u].pb({v , dis , 0});
		tr[v].pb({u , dis , 0});
	}
	for(int i = 1 ; i <= n ; i ++){
		cin >> cost[i];
	}
	dij(1);
	int ans = llinf;
	for(int i = 0 ;i <= 1000 ;  i++){
		ans = min(ans , dis[n][i]);
	}
	cout << ans << endl;
}            
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

        

        

### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参与并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值