Codeforces Global Round 4 D,E,F1

本文介绍了Codeforces Global Round 4中两道与构造有关的题目——D题Prime Graph和E题Archaeology。Prime Graph要求构造一个简单无向图,满足节点数为n,边数为质数,且每个节点度数也为质数。E题考古学问题涉及找到一个字符串的回文子序列,该子序列包含原始字符串至少一半的字符。文章提供了思路和解决方案,对于D题,可以通过构建环状图并连接前后半部分节点来满足条件;对于E题,发现字符串中左右相邻字符的匹配性,可以确定存在符合条件的子序列。
摘要由CSDN通过智能技术生成

链接: http://codeforces.com/contest/1178
D. Prime Graph(构造)
题面:
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn’t think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!

When building the graph, he needs four conditions to be satisfied:

It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops.
The number of vertices must be exactly n — a number he selected. This number is not necessarily prime.
The total number of edges must be prime.
The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime.
Below is an example for n=4. The first graph (left one) is invalid as the degree of vertex 2 (and 4) equals to 1, which is not prime. The second graph (middle one) is invalid as the total number of edges is 4, which is not a prime number. The third graph (right one) is a valid answer for n=4.
题意: 让你构造有n个点的图,使总边数是质数,每一个节点的度数也是质数
思路: 首先有一个性质就是n~(n+n/2)肯定存在一个质数,所以我们可以先把所有点连接起来构造一个环,接着让前n/2个节点与后n/2个节点相连,这样就构成了符合条件的图,每个点的度数为2或3,且总边数为质数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
bool prime(int x)
{
	if(x<2)
		return 0;
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n;
	scanf("%d",&n);
	int nn=n;
	for(;;nn++)
	{
		if(prime(nn))
		{
			break;
		}
	}
	printf("%d\n",nn);
	for(int i=1;i<n;i++)
	{
		printf("%d %d\n",i,i+1);
	}
	printf("%d %d\n",1,n);
	for(int i=1;i<=nn-n;i++)
	{
		printf("%d %d\n",i,i+n/2);
	}
	return 0;
}

E. Archaeology(贪心+构造)
题面:
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor’s Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?

The book contains a single string of characters “a”, “b” and “c”. It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides.

Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don’t have to maximise the length of it.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.
题意: 给你一段不连续相同且只含a,b,c三种字符的字符串,问是否能构造出来大于等于原串长度的子序列
思路: 首先我们要想为什么出题人把字符串限制为不连续相同且只含三种字符,所以多画几下,我们可以发现从左向右每两个肯定与从右往左每两个有一个字符相同,所以肯定存在符合条件的子序列,

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	string a;
	int l=0,r=s.size()-1;
	char ch;
	while(l<=r)
	{
		if(r-l+1<4)
		{
			ch=s[l];
			break;
		}
		if(s[l]==s[r])
		{
			ch=s[l];
		}
		else if(s[l]==s[r-1])
		{
			ch=s[l];
		}
		else
			ch=s[l+1];
		a.push_back(ch);
		l+=2,r-=2;
	}
	cout<<a;
	if(r-l+1<4&&l<=r)
		cout<<ch;
	reverse(a.begin(),a.end());
	cout<<a<<endl;
	return 0;
}

F1. Short Colorful Strip(区间dp)
链接: https://blog.csdn.net/qq_42819598/article/details/96897852

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值