Timus 1003. Parity

Timus 1003. Parity 是和奇偶校验相关的题目。

1003. Parity

Time Limit: 2.0 second
Memory Limit: 16 MB
Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on.
Your task is to guess the entire sequence of numbers. You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

Input

Input contains a series of tests. The first line of each test contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1 000 000 000. In the second line, there is one non-negative integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5 000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either “ even” or “ odd” (the answer, i.e. the parity of the number of ones in the chosen subsequence, where “ even” means an even number of ones and “ odd” means an odd number). The file is ended with a line containing −1.

Output

Each line of output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X + 1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

Sample

inputoutput
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
-1
3
Problem Source: Central European Olympiad in Informatics 1999
解答如下:
 1  using  System;
 2  using  System.IO;
 3  using  System.Collections.Generic;
 4 
 5  namespace  Skyiv.Ben.Timus
 6  {
 7     //   http://acm.timus.ru/problem.aspx?space=1 &num=1003
 8     sealed   class  T1003
 9    {
10       struct  Tuple < T1, T2 >
11      {
12         public  T1 A;
13         public  T2 B;
14         public  Tuple(T1 a, T2 b) { A  =  a; B  =  b; }
15      }
16      
17       struct  Tuple < T1, T2, T3 >
18      {
19         public  T1 A;
20         public  T2 B;
21         public  T3 C;
22      }
23      
24       static   void  Main()
25      {
26         new  T1003().Run(Console.In, Console.Out);
27      }
28      
29       void  Run(TextReader reader, TextWriter writer)
30      {
31         while  (reader.ReadLine()  !=   " -1 " ) writer.WriteLine(Find(Read(reader)));
32      }
33      
34      Tuple < int int bool > [] Read(TextReader reader)
35      {
36        Tuple < int , int , bool > [] ts  =   new  Tuple < int , int , bool > [ int .Parse(reader.ReadLine())];
37         for  ( int  i  =   0 ; i  <  ts.Length; i ++ )
38        {
39           string [] ss  =  reader.ReadLine().Split();
40          ts[i].A  =   int .Parse(ss[ 0 ])  -   1 ;
41          ts[i].B  =   int .Parse(ss[ 1 ]);
42          ts[i].C  =  (ss[ 2 ==   " odd " );
43        }
44         return  ts;
45      }
46      
47       int  Find(Tuple < int int bool > [] ts)
48      {
49         int [] q  =   new   int [ts.Length  *   2 ];
50         for  ( int  i  =   0 ; i  <  ts.Length; i ++ )
51        {
52          q[i  *   2 =  ts[i].A;
53          q[i  *   2   +   1 =  ts[i].B;
54        }
55        Distinct( ref  q);
56        Array.Sort(q);
57        Tuple < int bool > [] tree  =   new  Tuple < int , bool > [q.Length];
58         for  ( int  i  =   0 ; i  <  tree.Length; i ++ ) tree[i].A  =  i;
59         for  ( int  i  =   0 ; i  <  ts.Length; i ++ )
60        {
61           int  n  =  Array.BinarySearch(q, ts[i].B);
62          Tuple < int bool >  t2  =  FindRoot(tree, n);
63          Tuple < int bool >  t1  =  FindRoot(tree, Array.BinarySearch(q, ts[i].A));
64           if  (t1.A  !=  t2.A) tree[t1.A]  =   new  Tuple < int , bool > (n, t1.B  ^  ts[i].C);
65           else   if  (t1.B  ^  t2.B  ^  ts[i].C)  return  i;
66        }
67         return  ts.Length;
68      }
69      
70      Tuple < int bool >  FindRoot(Tuple < int bool > [] tree,  int  n)
71      {
72        Tuple < int bool >  result  =   new  Tuple < int bool > ();
73         for  (; n  !=  tree[n].A; n  =  tree[n].A) result.B  ^=  tree[n].B;
74        result.A  =  n;
75         return  result;
76      }
77      
78       void  Distinct < T > ( ref  T[] array)
79      {
80        Dictionary < T,  object >  dict  =   new  Dictionary < T,  object > ();
81         foreach  (T v  in  array) dict[v]  =   null ;
82        dict.Keys.CopyTo(array,  0 );
83        Array.Resize( ref  array, dict.Count);
84      }
85    }
86  }
87  /*  
88  Got Ac by using serious algorithm of building spanning forest 
89  in bipartite graph of segment’s ends only.
90  All simple “sport’s” attempts are failed because of TLE.
91  Fist mistake in answers found when meet chord [a,b] in some 
92  tree of the forest but dist[a]+dist[b]+ves([a,b]) is odd;
93  Dist[]- Boolean function of distances from roots of trees to their nodes.
94  Basic operation- link of two trees when new arc [a,b] connects to different trees.
95  For each node v tree[v]- tree, containing v- helpful array.
96  For linking trees used vector<int>Smeg[5000]- as dynamic structure of the Graph.
97  And BFS when second tree linking to the first one.
98  */

 

这道题是说,你和你的朋友玩一个游戏。你的朋友写下一个只包含“0”和“1”的序列,而你选择一个其中的一个子序列问你的朋友,该子序列中“1”的数目是奇数还是偶数?你的朋友回答后,你可以再选择一个子序列继续提问。但是你怀疑你的朋友的答案不全是正确的。因此你决定写一个程序来证实这一点。

程序的输入包含一系列案例。每个案例的第一行包含一个数,指示该序列的长度(不大于十亿)。第二行是一个不大于五千的非负整数,表示你提问的次数。剩余的行代表你的提问和你朋友的回答,每行由两个整数(分别表示子序列的开始和结束位置)和一个英文单词(“even”或“odd”,表示该子序列的奇偶性)构成。输入文件的最后一行包含“-1”。

对于每个案例,程序都在单独一行输出一个数,表示开头有多少个回答是正确的。

上述程序第 34 至 45 行的 Read 方法读取输入。然后调用第 47 至 68 行的 Find 方法寻找答案。第 78 至 84 行的 Distinct 方法用于删除数组中的重复元素。


返回目录
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于激光测速需要连续测量物体在一段时间内的位移,因此需要在一定时间间隔内进行多次测量。为了实现这一功能,我们可以使用STM32的定时器中断来触发测量,并通过串口将测量结果输出。 以下是基于STM32单片机使用激光测距模块MS53L1M进行激光测速的代码,注释已添加: ``` #include "stm32f1xx_hal.h" #include "stdio.h" // 定义串口句柄 UART_HandleTypeDef huart1; // 定义定时器句柄 TIM_HandleTypeDef htim2; // 定义激光测距模块的地址 #define LASER_ADDR 0x52 // 定义测量结果缓存区 uint8_t result[3]; // 定义测量时间间隔(单位:ms) #define MEASURE_INTERVAL 100 // 定义测量次数 #define MEASURE_TIMES 10 // 定义计数器 uint8_t count = 0; // 定义速度变量 float speed = 0; // 串口发送函数 void UART_Send_String(char* string) { HAL_UART_Transmit(&huart1, (uint8_t*)string, strlen(string), 100); } // 串口发送浮点数函数 void UART_Send_Float(float f) { char buffer[50]; sprintf(buffer, "%.2f", f); UART_Send_String(buffer); } // 定时器中断处理函数 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim->Instance == htim2.Instance) { // 每隔一定时间间隔进行一次测量 if(count == 0) { // 发送测量命令 uint8_t command[2] = {0x00, 0x04}; HAL_I2C_Master_Transmit(&hi2c1, LASER_ADDR, command, 2, 100); // 等待测量完成 HAL_Delay(10); // 读取测量结果 HAL_I2C_Master_Receive(&hi2c1, LASER_ADDR, result, 3, 100); // 计算测量距离 float distance = ((result[0] << 8) | result[1]) / 100.0; // 计算速度 if(count == MEASURE_TIMES - 1) { speed = (MEASURE_TIMES * distance) / (MEASURE_INTERVAL * (MEASURE_TIMES - 1)); } // 发送测量结果 UART_Send_Float(distance); UART_Send_String(" cm\r\n"); } count++; // 如果已经测量了足够次数,则输出速度并清零计数器 if(count >= MEASURE_TIMES) { UART_Send_String("Speed: "); UART_Send_Float(speed); UART_Send_String(" cm/s\r\n"); count = 0; } } } int main(void) { // 初始化HAL库 HAL_Init(); // 初始化串口 huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart1); // 初始化定时器 htim2.Instance = TIM2; htim2.Init.Prescaler = 7200 - 1; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = MEASURE_INTERVAL - 1; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; HAL_TIM_Base_Init(&htim2); HAL_TIM_Base_Start_IT(&htim2); // 初始化I2C总线 hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; HAL_I2C_Init(&hi2c1); // 启动循环 while (1) { } } ``` 需要注意的是,以上代码假定使用的是STM32F103单片机,串口使用的是USART1,I2C总线使用的是I2C1。如果使用其他型号的单片机或不同的串口和I2C总线,需要对代码进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值