Codeforces Round #686 (Div. 3)(全部AC代码和E.F两题详细题解)

A. Special Permutation

You are given one integer n (n>1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation of length 5, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1≤i≤n) such that pi=i (so, for all i from 1 to n the condition pi≠i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n>1.
Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2≤n≤100) — the length of the permutation you have to find.
Output
For each test case, print n distinct integers p1,p2,…,pn — a permutation that there is no index i (1≤i≤n) such that pi=i (so, for all i from 1 to n the condition pi≠i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n>1.
输入:
2
2
5
输出:
2 1
2 1 5 3 4

AC代码:

#include<iostream>
using namespace std;
int a[105];
int main(){
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=1;i<n;i++)
		a[i]=i+1;
		a[n]=1;
		for(int i=1;i<=n;i++)
		cout<<a[i]<<" ";
		cout<<endl;
	}
}

B. Unique Bid Auction

There is a game called “Unique Bid Auction”. You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don’t have to do it to solve this problem).
Let’s simplify this game a bit. Formally, there are n participants, the i-th participant chose the number ai. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2e4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2e5) — the number of participants. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2e5 (∑n≤2e5).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
输入:
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
输出:
-1
2
4
1
2
-1

AC代码:


#include <bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vector<int> cnt(n + 1), idx(n + 1);
		for (int i = 0; i < n; ++i) {
			int x;
			cin >> x;
			++cnt[x];
			idx[x] = i + 1;
		}
		int ans = -1;
		for (int i = 0; i <= n; ++i) {
			if (cnt[i] == 1) {
				ans = idx[i];
				break;
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

C. Sequence Transformation

You are given a sequence a, initially consisting of n integers.
You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).
To achieve this, you choose some integer x that occurs at least once in a, and then perform the following operation any number of times (possibly zero): choose some segment [l,r] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains x. More formally, you choose some contiguous subsequence [al,al+1,…,ar] such that ai≠x if l≤i≤r, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the (r+1)-th is now l-th, the element that was (r+2)-th is now (l+1)-th, and so on (i. e. the remaining sequence just collapses).
Note that you can not change x after you chose it.
For example, suppose n=6, a=[1,3,2,4,1,2]. Then one of the ways to transform it in two operations is to choose x=1, then:
choose l=2, r=4, so the resulting sequence is a=[1,1,2];
choose l=3, r=3, so the resulting sequence is a=[1,1].
Note that choosing x is not an operation. Also, note that you can not remove any occurrence of x.
Your task is to find the minimum number of operations required to transform the sequence in a way described above.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2e4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2e5) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a.
It is guaranteed that the sum of n does not exceed 2e5 (∑n≤2e5).
Output
For each test case, print the answer — the minimum number of operations required to transform the given sequence in a way described in the problem statement. It can be proven that it is always possible to perform a finite sequence of operations so the sequence is transformed in the required way.
输入:
5
3
1 1 1
5
1 2 3 4 5
5
1 2 3 2 1
7
1 2 3 1 2 3 1
11
2 2 1 2 3 2 1 2 3 1 2
输出:
0
1
1
2
3

AC代码:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int main(){
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		int a[n+4],b[n+4];
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int minn=inf;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]!=a[i-1]) b[a[i]]++;
		}
		b[a[1]]--;b[a[n]]--;
		for(int i=1;i<=n;i++) minn=min(minn,b[a[i]]);
		cout<<minn+1<<endl;
	}
	return 0;
}

D. Number into Sequence

You are given an integer n (n>1).
Your task is to find a sequence of integers a1,a2,…,ak such that:
each ai is strictly greater than 1;
a1⋅a2⋅…⋅ak=n (i. e. the product of this sequence is n);
ai+1 is divisible by ai for each i from 1 to k−1;
k is the maximum possible (i. e. the length of this sequence is the maximum possible).
If there are several such sequences, any of them is acceptable. It can be proven that at least one valid sequence always exists for any integer n>1.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤5000) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2≤n≤1e10).
It is guaranteed that the sum of n does not exceed 1010 (∑n≤1e10).
输入:
4
2
360
4999999937
4998207083
输出:
1
2
3
2 2 90
1
4999999937
1
4998207083

AC代码:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll t,n,cnt,ans;
pair<ll,ll>p; //记录质因数的个数以及数值 
int main(){
	scanf("%d",&t);
	while(t--){
		p.first=p.second=0;
		cin>>n;
		ans=n;
		for(ll i=2;i*i<=n;i++){
			int cnt=0;
		   while(n%i==0) {
		     cnt++;
		     n/=i;
		   }
		   if(cnt>p.first){
		      p.first=cnt;
		      p.second=i;
		      cnt=0;
		   }
		}
		if(p.first==0) cout<<1<<"\n"<<n<<endl;
		else{
			cout<<p.first<<endl;
			for(ll i=1;i<p.first;i++)
			{
			   ans/=p.second;
			   cout<<p.second<<" ";
			}
			cout<<ans<<endl;
		}
	}
	return 0;
} 

E.Number of Simple Paths

You are given an undirected graph consisting of n vertices and n edges. It is guaranteed that the given graph is connected (i. e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph.
Your task is to calculate the number of simple paths of length at least 1 in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). For example, paths [1,2,3] and [3,2,1] are considered the same.
You have to answer t independent test cases.
Recall that a path in the graph is a sequence of vertices v1,v2,…,vk such that each pair of adjacent (consecutive) vertices in this sequence is connected by an edge. The length of the path is the number of edges in it. A simple path is such a path that all vertices in it are distinct.
Input
The first line of the input contains one integer t (1≤t≤2e4) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (3≤n≤2e5) — the number of vertices (and the number of edges) in the graph.
The next n lines of the test case describe edges: edge i is given as a pair of vertices ui, vi (1≤ui,vi≤n, ui≠vi), where ui and vi are vertices the i-th edge connects. For each pair of vertices (u,v), there is at most one edge between u and v. There are no edges from the vertex to itself. So, there are no self-loops and multiple edges in the graph. The graph is undirected, i. e. all its edges are bidirectional. The graph is connected, i. e. it is possible to reach any vertex from any other vertex by moving along the edges of the graph.
It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2e5).
Output
For each test case, print one integer: the number of simple paths of length at least 1 in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths).
输入:
3
3
1 2
2 3
1 3
4
1 2
2 3
3 4
4 2
5
1 2
2 3
1 3
2 5
4 3
输出:
6
11
18

这是一道基环树的题

分析题意:
对于一棵n个结点n-1条边的树,由于每两个结点之间只有一条路径,且由于是无向图,则总路径为n*(n-1)/2; 而对于一个有n个结点的环来说,由于每两个点之间有两条路径,则总路径为n*(n-1)
由于该题是一个n个结点n条边的无向连通图,显然是一棵普通的树加上一条边,使其拥有一个环,所以是基环树。
两种做法:

做法一:
对于环上每一个点,都可以看成是一棵独立的小树的树根,记录该小树的顶点数为v[i],则对于该小树来说,总路径为v[i]*(v[i]-1)/2;而对于小树的上的点到其他点所有路径为v[i]*(n-v[i]);(由于小树上的点到其他点需要经过一个环,所以需要乘2,于是乘2除2抵消了)。总路径就是遍历所有环上的点,然后加上每个点的这两种路径。
做法二:
由于图上任意两点之间的路径条数只能为1或者2,即两个结点的路径之间没有环就是1,两个结点的路径之间有环则就是2.所以我们可以把整个图看成一个大环,n个结点都在环上,则总路径为n*(n-1); 然后找到所有的小树上的路径条数,也就是小树上的顶点数sz[i]*(sz[i]-1)/2;然后减去它即可。


其中用的算法
基环树找环两种算法:1.深搜dfs 2.拓朴排序
找到小树上的顶点数:遍历每个环上的点再dfs
注意:bool数组判断比int数组判断效率高,本题用int可能会超时
AC代码:

include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10; 
int n,fa[maxn],t[maxn],tot;
bool vis[maxn],loop[maxn]; //k用来记录以环上的点为树根建的小树的顶点数 
vector<int>g[maxn];
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return x*f;
} 
void Init(){
	memset(loop,0,sizeof(loop));
	memset(vis,0,sizeof(vis));
	memset(t,0,sizeof(t));
	tot=0;
	for(int i=1;i<=n;i++) {fa[i]=i;g[i].clear();}
}
void get_loop(int u){
	t[u]=++tot;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(fa[u]==v) continue;
		if(t[v]){
			if(t[v]<t[u]) continue;
			loop[v]=1;
			for(;v!=u;v=fa[v]) loop[fa[v]]=1;
		}
		else {fa[v]=u;get_loop(v);}
	}
}
int dfs(int u){
	int result=1;
	vis[u]=1;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(loop[v]||vis[v]) continue;
		result+=dfs(v);
	}
	return result;
}
int main(){
	int T;
	T=read();
	while(T--){
		n=read();
		Init();
		for(int i=1;i<=n;i++){
			int u,v;
			u=read();v=read();
			g[u].push_back(v);
			g[v].push_back(u);
		}ll ans=0;
		get_loop(1);
        for(int i=1;i<=n;i++){
        	if(loop[i]){
        		ll k=dfs(i);
        		ans+=k*(n-k)+k*(k-1)/2;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

F. Array Partition

You are given an array a consisting of n integers.

Let min(l,r) be the minimum value among al,al+1,…,ar and max(l,r) be
the maximum value among al,al+1,…,ar.

Your task is to choose three positive (greater than 0) integers x, y
and z such that:

x+y+z=n; max(1,x)=min(x+1,x+y)=max(x+y+1,n). In other words, you have
to split the array a into three consecutive non-empty parts that cover
the whole array and the maximum in the first part equals the minimum
in the second part and equals the maximum in the third part (or
determine it is impossible to find such a partition).

Among all such triples (partitions), you can choose any.

You have to answer t independent test cases.

Input The first line of the input contains one integer t (1≤t≤2e4) —
the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (3≤n≤2e5) —
the length of a.

The second line of the test case contains n integers a1,a2,…,an
(1≤ai≤1e9), where ai is the i-th element of a.

It is guaranteed that the sum of n does not exceed 2e5(∑n≤2e5).

Output For each test case, print the answer: NO in the only line if
there is no such partition of a that satisfies the conditions from the
problem statement. Otherwise, print YES in the first line and three
integers x, y and z (x+y+z=n) in the second line.

If there are several answers, you can print any.
输入:
6
11
1 2 3 3 3 4 4 3 4 2 1
8
2 9 1 7 3 9 4 1
9
2 1 4 2 4 3 3 1 2
7
4 2 1 1 4 1 4
5
1 1 1 1 1
7
4 3 4 3 3 3 4
输出
YES
6 1 4
NO
YES
2 5 2
YES
4 1 2
YES
1 1 3
YES
2 1 4

分析题意
由于该题需要把一段序列分成三份,并分别求其最值,使之相等,显然要用区间求最值的算法(ST表,预处理时间复杂度O(nlonn),查询时间复杂度O(1); 线段树预处理时间复杂度比较小,但是查询的时间复杂度是O(logn) );对于本题总时间复杂度都是O(nlogn),因为本题只需要查询。对于第一段的尾结点x,我们可以从1~n-2开始遍历,此时由于第一段的区间确定了就是[1 ,x],则最值也确定了。而我们接下来的工作就是找到第二段的尾结点y即可。那么我们应该怎么找到y的位置呢?我们知道;对于一段区间的最小值,随着区间的扩张会变小或者不变;对于一段区间的最大值,随着区间的扩张会变大或者不变。那我们是不是可以通过二分法找到结点y呢?当然是可以的,由于第一段最大值已经确定了,我们只需要找到第二段的最小值和第一段的最大值的关系,以及第三段的最大值和第一段的最大值即可。显然有5种情况:因为结点y只可能在区间[x+1,n-1] 之内,那么我们就从这个区间开始二分。设l=x+1,r=n-1,则mid=(l+r)/2 ;记第一个区间[1,x]最大值为max1,第二个区间[x+1,mid]最小值为min,第三个区间最大值[mid+1,n]为max2;

情况一: max1< min 即第二个区间最小值偏大,我们需要扩张第二个区间,则l=mid+1;
情况二: max1>min 即第二个区间偏小,我们需要缩小第二个区间,则r=mid-1。
情况三: max1<max2 即第三个区间偏大,需要缩小第三个区间,则l=mid+1。
情况四:max1>max2 即第三个区间偏小,需要扩张第三个区间,则r=mid-1。
情况五:max1==min ==max2 则找到了答案,break.


其中用的算法:
区间求最值rmq:1.ST表 2.线段树
遍历第一段和第二段的分割点x,再通过二分法找到第二段和第三段之间的分割点y.
AC代码:

ST表:
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=2e5+10;
const int maxx=100;
int a[maxn],Log[maxn],b[40],dmin[maxx][maxn],dmax[maxx][maxn];
int n;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
void Init(){
	b[0]=1;
	for(int i=1;i<=20;i++) b[i]=b[i-1]*2;
	Log[0]=-1;
	for(int i=1;i<=n;i++) Log[i]=Log[i/2]+1;
	for(int i=1;i<=n;i++) dmin[0][i]=dmax[0][i]=a[i];
	for(int i=1;i<=Log[n];i++)
	    for(int j=1;j<=n;j++)
	    if(j+b[i]-1<=n) dmin[i][j]=min(dmin[i-1][j],dmin[i-1][j+b[i-1]]),dmax[i][j]=max(dmax[i-1][j],dmax[i-1][j+b[i-1]]);
}
inline int getmax(int l,int r){
	int k=Log[r-l+1];
	return max(dmax[k][l],dmax[k][r-b[k]+1]);
}
inline int getmin(int l,int r){
	int k=Log[r-l+1];
	return min(dmin[k][l],dmin[k][r-b[k]+1]);
}
int main(){
	int t;t=read();
	while(t--){
		n=read();
		for(int i=1;i<=n;i++) a[i]=read();
		Init();
		bool flag=0;
		for(int i=1;i<=n-2;i++){
			int ans1=getmax(1,i);
			int l=i+1,r=n-1,mid,vis=0;
			while(l<=r){
		        mid=(l+r)/2;
		        int ans2=getmin(i+1,mid),ans3=getmax(mid+1,n);
		        if(ans1<ans2||ans1<ans3) l=mid+1;
		        else if(ans1>ans2||ans1>ans3) r=mid-1;
		        else {
		        	cout<<"YES\n"<<i<<" "<<mid-i<<" "<<n-mid<<endl;
		        	vis=flag=1;
		        	break;
				}
		    }
		    if(vis) break;
		}
		if(!flag) cout<<"NO"<<endl;
	}
	return 0;
} 
线段树:
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=2e5+10;
const int maxx=100;
int a[maxn],n;

struct Node{
	int l,r,maxx,minn;
}tree[maxn*4];

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline void up(int k){
	tree[k].maxx=max(tree[2*k].maxx,tree[2*k+1].maxx);
	tree[k].minn=min(tree[2*k].minn,tree[2*k+1].minn);
}
void build(int k,int ll,int rr){
	tree[k].l=ll,tree[k].r=rr;
	if(ll==rr){
		tree[k].maxx=tree[k].minn=a[ll];
		return ;
	}
	int m=(ll+rr)/2;
	build(2*k,ll,m);
	build(2*k+1,m+1,rr);
	up(k);
}
inline int getmax(int k,int ll,int rr){
	if(tree[k].l>rr||tree[k].r<ll)
		return -INF;
	if(ll<=tree[k].l&&rr>=tree[k].r){
		return tree[k].maxx;
	}
	return max(getmax(2*k+1,ll,rr),getmax(k*2,ll,rr));
}
inline int getmin(int k,int ll,int rr){
	if(tree[k].l>rr||tree[k].r<ll)
		return INF;
	if(ll<=tree[k].l&&rr>=tree[k].r){
		return tree[k].minn;
	}
	return min(getmin(k*2,ll,rr),getmin(2*k+1,ll,rr));
}
int main(){
	int t;t=read();
	while(t--){
		n=read();
		for(int i=1;i<=n;i++) a[i]=read();
		build(1,1,n);
		bool flag=0;
		for(int i=1;i<=n-2;i++){
			int ans1=getmax(1,1,i);
			int l=i+1,r=n-1,mid,vis=0;
			while(l<=r){
		        mid=(l+r)/2;
		        int ans2=getmin(1,i+1,mid),ans3=getmax(1,mid+1,n);
		        if(ans1<ans2||ans1<ans3) l=mid+1;
		        else if(ans1>ans2||ans1>ans3) r=mid-1;
		        else {
		        	cout<<"YES\n"<<i<<" "<<mid-i<<" "<<n-mid<<endl;
		        	vis=flag=1;
		        	break;
				}
		    }
		    if(vis) break;
		}
		if(!flag) cout<<"NO"<<endl;
	}
	return 0;
} 
  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&が&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值