【H - Parity game】

本文介绍了一种结合种类并查集与离散化的高效算法解决方案,针对大规模数据集的处理进行了详细解析。通过使用STL函数进行排序和去重,实现对1e9级别的数字范围的有效管理。文章分享了具体的代码实现,包括初始化、查找、并集操作等关键步骤,展示了如何利用离散化方法减少内存占用,提高处理速度。

思路:

  • 关于本文中首次用到的一些STL函数,详见:FORMER_BLOG
  • 种类并查集+离散化
  • 这应该是一种非常简便的离散化方法:
    1. 因为数字的范围太大(1e9),不得不离散化,即将 5000*2index 通过映射存起来。
    2. 把每个询问存进结构体,数组和队列皆可;把全部的端点 index 存进 f 数组parvalmaxn 都是 1e4 + 5)。
    3. fsort 排序,unique 去重,如此,每个端点 index 就有了自己的 “指纹”
    4. 对每个询问,使用 lower_bound 寻找它的指纹,将它的指纹作为 l、r 进行并查集。

代码:

  • 141ms 912kB
//141ms		912kB


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

const int maxn = 1e4 + 5;

int N,M;
int ans;
int par[maxn];
int val[maxn];
int f[maxn];int cnt;//存储所有用到的index 
struct NODE{
	int l,r,w;
	NODE(int l,int r,int w) : l(l) , r(r) , w(w) {} ;
};
queue<NODE> Q;

void INIT(){
	ans = 0;
	cnt = 0;
	memset(par , -1 , sizeof(par));
	memset(val , 0  , sizeof(val));
	return ;
}

int FIND(int i){
	if(par[i] == -1)
		return i;
	int tp = par[i];
	par[i] = FIND(par[i]);
	val[i] = (val[i] + val[tp]) % 2;
	return par[i];
}

void UNION(int w,int l,int r,int parl,int parr){
	par[parr] = parl;
	val[parr] = (val[l] + w - val[r] + 2) % 2;
	return ;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	while(cin>>N>>M){
		INIT();
		for(int i=0;i<M;i++){
			int l,r,w;
			string str;
			cin>>l>>r>>str;
			if(str.size() == 3)
				w = 1;
			else
				w = 0;
			l--;
			Q.push(NODE(l,r,w));
			f[cnt++] = l;
			f[cnt++] = r;
		}
		sort(f , f+cnt);//排序
		int n = unique(f , f+cnt) - f;//1,2,3,4,5,1,1,2,2,3去重,得到去重后的元素个数
		while(Q.size()){
			NODE cur = Q.front() ; Q.pop() ;
			if(ans)
				continue;
			int w = cur.w ;
			int l = lower_bound(f , f+n , cur.l) - f;
			int r = lower_bound(f , f+n , cur.r) - f;//得到下标
			int parl = FIND(l);
			int parr = FIND(r);
			if(parl == parr)
				if((val[l] + w)%2 != val[r])
					ans = M - Q.size() - 1 ;
				else
					;
			else
				UNION(w , l , r , parl , parr);
		}
		if(!ans)
			ans = M;
		cout<<ans<<endl;
	} 
	return 0;
}
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "dma.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "OLED.h" #include "PWM.h" #include "Delay.h" #include <math.h> #include <stdio.h> #include <string.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ uint8_t str_buf[32]; float Kp = 0,Ki = 0,Kd = 0; uint8_t pid_state; HAL_StatusTypeDef test_state; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_TIM1_Init(); MX_TIM2_Init(); MX_USART2_UART_Init(); MX_USART3_UART_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ Servo_Start(); OLED_Init(); HAL_UARTEx_ReceiveToIdle_DMA(&huart2, usart2.rx_data, 3); __HAL_DMA_DISABLE_IT(usart2.hdma_rx, DMA_IT_HT); HAL_TIM_Base_Start_IT(&htim2); OLED_ShowString(1,1,"success"); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if(tim2_IT_flag == 1) { tim2_IT_flag = 0; } if(usart2.rx_flag == 1) { usart2.rx_flag = 0; int i = 0; int state = 0; for(i = 0;i < sizeof(usart2.rx_data_copy); i++) { if(state == 0 && usart2.rx_data_copy[i] == 0xFF) { state = 1; } else if(state == 1) { pid_state = usart2.rx_data_copy[i]; state = 0; test_state = HAL_UART_Transmit_DMA(&huart2, &(usart2.rx_data_copy[i]),1); } } } switch (pid_state) { case 1: Kp += 0.1; break; } OLED_ShowHexNum(2,1,pid_state,2); OLED_ShowHexNum(3,1,test_state,2); sprintf((char*)str_buf,"%f", Kp); OLED_ShowString(4,1,(char*)str_buf); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ 解读代码
最新发布
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值