Description
N pudding is placed in a row for M operations. Each time a color's pudding is all turned into another color, then ask the current total number of colors. For example, four pudding with a color of 1,2,2,1, a total of 3 colors.
Input
The first line gives N,m the number of pudding and the number of times a friend is operating. Second row n number a1,a2 ... An indicates that the color of the first I pudding has M line from the third line, and for each operation, if the first number is 1 to change the color, then the two integer, x, y, or X, indicates that turning all colors into y,x may be equal to Y. If the first number is 2 to ask how many colors are currently in question, you should output an integer. 0
Output
Ask for the second type of action, and then output the current number of colors.
Sample Input4 3
1 2 2 1
2
1 2 1
2
Sample Output3
1
Hintsource
Analysis
The analysis of the complexity of the heuristic merging: Orzz
Each time we combine short to Long, O (short length) does not make much difference, let us look at the situation of averaging: 1: Each O (N) 2: After each merge, the queue length must be greater than or equal to the original short length of twice times. This equates to a short length increase of more than one time per merger, up to a maximum of logn times, so the total complexity O (nlogn), each o (logn). It's a naked question. Make a list to record the beginning of each color segment, and then modify the brute force change + heuristic merge every time. And remember to use an array map for colors that cause errors due to size.
1 /*2 The nature of the German3 If life is just like the beginning, what autumn wind sad painting fan. 4 It is easy to change but the heart of the heart, but the people change. 5 Lishan language is half a night, tears Lin Bell finally do not complain. 6 How Thin Fortunately Viceroy Lang, wing to the same day wish. 7 */8#include <iostream>9#include <cstdio>Ten#include <algorithm> One#include <cstring> A#include <vector> -#include <utility> -#include <iomanip> the#include <string> -#include <cmath> -#include <queue> -#include <assert.h> +#include <map> -#include <ctime> +#include <cstdlib> A#include <stack> at #defineLOCAL - Const intMAXN =100000+Ten; - Const intINF =100000000; - Const intSIZE = the; - Const intMAXM =1000000+Ten; - Const intMaxnode =0x7fffffff+Ten; in using namespacestd; - structnode{ to intnum; +Node *Next; -}*HEAD[MAXM];//Head is the table header the intCNT[MAXM], REM[MAXM], DATA[MAXM]; * inttot, N, m; $ Panax Notoginseng //Add a node with a color x in the list - voidAddintXintD) {//D stands for location the if(Cnt[x] = =1){ +HEAD[X] =NewNode; AHead[x]->num =D; theHead[x]->next =NULL; +}Else{ - //Insert in table header $Node *p =NewNode; $P->num =D; -P->next =Head[x]; -HEAD[X] =p; the } - }Wuyi voidinit () { thetot =0;//total number of record colors -memset (CNT,0,sizeof(CNT));//record the number of colors Wuscanf"%d%d", &n, &m); - //for (int i = 1; I <= n; i++) rem[i] = i; Aboutdata[0] = -INF; $ for(inti =1; I <= N; i++){ -scanf"%d", &data[i]);//Input Color - if(Data[i]! = data[i-1]) tot++; -cnt[data[i]]++; A Add (Data[i], i); +Rem[data[i]] = data[i];//error-proof array initialization the } - $ } the //Change a color to B color the voidChangeintAintb) { the //don't make a mistake. Is the number of the correct color the if(Cnt[rem[a]) >Cnt[rem[b]]) swap (Rem[a], rem[b]); -A = Rem[a];//always let the number of colors become much less. inb =Rem[b]; the if(Cnt[a] = =0)return; theCNT[B] + =Cnt[a]; AboutCnt[a] =0; theNode *cur; the for(cur = head[a]; cur! = NULL; cur = cur->next) { the if(Data[cur->num +1] = = b) tot--; + if(Data[cur->num-1] = = b) tot--; - } the for(cur = head[a]; Cur->next! = NULL; cur = cur->next) Data[cur->num] =b;BayiData[cur->num] =b; the //Finally, insert a in the back of B . theCur->next =Head[b]; -HEAD[B] =Head[a]; - } the voidWork () { the for(inti =1; I <= m; i++){ the intT, a, B; thescanf"%d", &t); - if(T = =2) printf ("%d\n", tot); the Else{ thescanf"%d%d", &a, &b); the if(A = = b)Continue;//two colors of the same94 Change (A, b); the } the } the //for (int i = 1; I <= n; i++) printf ("%d", Data[i]);98 } About - intMain () {101 102 init ();103 Work ();104 return 0; the}
View Code
"BZOJ1483" "Linked list heuristic merge" Fantasy pudding