当前位置:网站首页>Exchange bottles (graph theory + thinking)
Exchange bottles (graph theory + thinking)
2022-07-06 03:44:00 【AC__ dream】
sample input 1:
5
3 1 2 5 4
sample output 1:
3
sample input 2:
5
5 4 3 2 1
sample output 2:
2
The idea of this topic is difficult to think about , It is a problem combined with graph theory , We put this n A position can be regarded as n A little bit , For the first i The value of a point a[i], Jean di i Point to point a[i] A dot with a directed edge represents the i The final position of the value at point is a[i] A little bit , Because each point will only have a final position and the position will only be the final position of a point , So after connecting the edges according to the above method, there will only be one out edge and one in edge for each point , That is, it will form several rings , Follow the example first 1 Draw edges and analyze :
This is the graph built according to the above method , Now let's take a look at what changes will happen when we build a map after exchanging two points
At this time, the discussion should be divided into two situations , One is to exchange two points in a ring , The other is that the two points exchanged are located in two rings
Case one :( The two points of exchange are located in a ring , such as 1,2 Point on position )
be 1 The point on the position will point to 2 The point pointed by the position point , That is to say 1, and 2 The position will point to 1 The point the position points to , That is to say 3, So we'll find out 1 Forming a self ring , So the number of rings decreases 1
The second case :( The two points of exchange are located on two different rings , such as 1,5 Point on position )
be 1 The point on the position will point to 5 The point pointed by the position point , That is to say 4, and 5 The position will point to 1 The point the position points to , That is to say 3, In this way, we will find that the ring of these two points becomes a big ring , That is, the number of rings is reduced 1
Through the analysis of these two situations , We will find that if two points of the exchange are in a ring , Then the number of rings decreases 1, If two points of the exchange are on two different rings , Then the number of rings decreases 1, And our final situation is that all points form a self ring , That is, the number of rings is n, So we should exchange two points in a ring at a time to increase the number of rings 1. So the answer is n- At first, the number of rings
How to calculate the number of rings , A clever method is used here , Refer to code for details :
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=10003;
int a[N];
bool vis[N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int cnt=0;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
cnt++;
int t=i;
while(!vis[t])
{
vis[t]=true;
t=a[t];
}
}
}
printf("%d",n-cnt);
return 0;
}
边栏推荐
- [American competition] mathematical terms
- 自动化测试怎么规范部署?
- Pointer written test questions ~ approaching Dachang
- SAP ALV颜色代码对应颜色(整理)
- Mysqldump data backup
- BUAA calculator (expression calculation - expression tree implementation)
- 施努卡:视觉定位系统 视觉定位系统的工作原理
- A brief introduction to symbols and link libraries in C language
- An article will give you a comprehensive understanding of the internal and external components of "computer"
- EDCircles: A real-time circle detector with a false detection control 翻译
猜你喜欢
记录一下逆向任务管理器的过程
Factors affecting user perception
canvas切积木小游戏代码
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Blue Bridge Cup - day of week
1、工程新建
Getting started with applet cloud development - getting user search content
Canvas cut blocks game code
svg拖动点裁剪图片js特效
Alibaba testers use UI automated testing to achieve element positioning
随机推荐
[analysis of variance] single factor analysis and multi factor analysis
潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
2.1 rtthread pin设备详解
出现Permission denied的解决办法(750权限谨慎使用)
C#(二十八)之C#鼠标事件、键盘事件
施努卡:3d视觉检测应用行业 机器视觉3d检测
2.2 STM32 GPIO操作
Python implementation of maddpg - (1) openai maddpg environment configuration
On Data Mining
2.2 fonctionnement stm32 GPIO
SAP ALV cell level set color
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
SAP ALV颜色代码对应颜色(整理)
[practical exercise] face location model based on skin color
1.16 - check code
1. New project
Pytoch foundation - (1) initialization of tensors
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
cookie,session,Token 这些你都知道吗?
canvas切积木小游戏代码