链接: 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