C: This is said to be a set of baby, if you know when the problem is a set of baby, it is good to understand more
Rule 1: A set of baby A can be placed in the set of Baby B inside, when and only if the set of baby B is not placed in other sets of baby inside
Rule 2: Set the Baby A in the set of Baby B inside, and set the baby B is not placed in other sets of baby inside, then you can take a from B out
Ask us at least how many times to operate, in order to set the baby all up, disassembly and assembly are considered an operation
Idea: Find the number of sets of 1 of the baby's group, and then statistics the group has how many sets of baby is connected to 1, and each serial number is added 1, then these sets of baby is not to be disassembled. So the rest of the dolls are going to be taken apart.
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A#include <math.h> - using namespacestd; - #pragmaWarning (disable:4996) thetypedefLong LongLL; - Const intINF =1<< -; - /* - + */ - inta[100000+Ten]; + intMain () A { at - intN, K, m; - intans; - intCNT; - while(SCANF ("%d%d", &n, &k)! =EOF) - { inAns = CNT =0; - for(inti =0; I < K; ++i) to { +scanf"%d", &m); - BOOLFlag =false; the for(intj =0; J < M; ++j) * { $scanf"%d", &a[j]);Panax Notoginseng if(A[j] = =1) - { theFlag =true; + } A } the + if(flag) - { $CNT =1; $ for(intj =1; J < M; ++j) - { - if(A[j]-1= = A[j-1]) thecnt++; - ElseWuyi Break; the } -Ans + = m-CNT; WuFlag =false; - } About Else $Ans + = m-1; - } -Ans + = n-CNT; -printf"%d\n", ans); A } + return 0; the}
View Code
D: The idea was to sort the islands from small to large by distance from the next island, and to sort the length of the bridge from small to large, then greedy, but the question was
May be greedy, choose the current bridge A, but Bridge B also time itself (bridge a row in front of Bridge B), but, to the next island, Bridge b for it, too long.
Example: Three islands, two bridges, it's not good to be greedy as it looks.
1 10
11 16
20 23
10 15
Above the greedy strategy, the information that is not taken into account is the length of the island itself. Only the distances between islands and islands are considered
We stipulate that the upper bound is: the longest bridge that can be built between two islands, the lower bound is: the shortest bridge
We can sort the islands by the upper bounds and then select the bridge to find the first bridge greater than or equal to the nether. This will not happen (because the upper bound is incremented).
Another point is that the STL's Lower_bound () function, in extreme cases, has a time complexity of O (n), which times out
The length of the bridge can be stored in the set, and then the Lower_bound () function with set comes with a time complexity of O (LOGN)
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A#include <math.h> - using namespacestd; - #pragmaWarning (disable:4996) thetypedefLong LongLL; - Const intINF =1<< -; - /* - + */ - Const intN =200000+Ten; + struct Island A { at LL L, R, Upper, lower, id; - BOOL operator< (ConstISLAND&RHS)Const - { - returnUpper <Rhs.upper; - } - }island[n]; in Set<pair<ll,int> >b; - intAns[n]; to intMain () + { - intN, M; the LL A; *scanf"%d%d", &n, &m); $ for(inti =0; I < n; ++i)Panax Notoginseng { -scanf"%i64d%i64d", &ISLAND[I].L, &ISLAND[I].R); the } + for(inti =0; I < n-1; ++i) A { theIsland[i].id =i; +Island[i].lower = Island[i +1].L-ISLAND[I].R; -Island[i].upper = Island[i +1].R-ISLAND[I].L; $ } $Sort (island, Island + N-1); - for(inti =0; I < m; ++i) - { thescanf"%i64d", &a); - B.insert (Make_pair (A, i));Wuyi } the for(inti =0; I < n-1; ++i) - { Wu Set<pair<ll,int> >::iterator iter = B.lower_bound (Make_pair (Island[i].lower,0)); - if(iter = =b.end ()) About { $Puts"No"); - return 0; - } - Else A { + if(Iter->first <=island[i].upper) the { -Ans[island[i].id] = Iter->second +1; $ B.erase (ITER); the } the Else the { thePuts"No"); - return 0; in } the } the } AboutPuts"Yes"); theprintf"%d", ans[0]); the for(inti =1; I < n-1; ++i) theprintf"%d", Ans[i]); +Puts""); - return 0; the}
View Code
Target: ①div2 do 4 questions ② AC, hand fast, test instructions see
Codeforces round#310 Div2