Question 1: coated with bread
Description:
Apply the layer_count layer on the bread to make the bread with raspberry sauce. Considering that you can only paint the upper_limit layer at most each time, you must calculate the minimum number of times of dressing.
Analysis:
You only need to apply as much raspberry sauce as possible each time. This is a typical greedy idea.
Code:
Int Toastxraspberry: Apply ( Int Upper_limit, Int Layer_count ){
If (Layer_count % upper_limit = 0 ){
Return Layer_count/upper_limit;
}
Return Layer_count/upper_limit + 1
}
|
Question 2: bread
Description:
For each type of bread, there is a barbecue time X. If the bread is baked for exactly X minutes, it is called good bread. If it is less than X, it is not cooked; if it is more than X, it is burned. The baker baked some bread now. Because he does not know the exact barbecue time for each type of bread, it is burnt if it is not cooked. The only thing he remembers is that each type of bread is not cooked or burnt. Now you know the exact time when the uncooked bread and bread are baked, giving at least a few types of bread is used.
Analysis:
The question is hard to understand. The simple explanation is that if there is a kind of bread, the baker will burn it too if it is not cooked. Therefore, if X1 is the minimum roasting time, it must be later than the minimum not-cooked time Y1. Otherwise, only one type of bread is burned, but it is not cooked. Similarly, if X2 is the maximum roasting time, it is greater than the maximum roasting time Y2. Otherwise, the same type of bread is not scorched, conflict. If neither of the above conditions meets, at least one type of bread is used for baking. If X1 is greater than Y2, there is only one type of bread. Otherwise, the bread whose barbecue time t1 is smaller than X1 and greater than Y1 will be burned in the same time as that of X1. However, since T1 is smaller than Y2, at least another type of bread is baked. Since X2 is greater than Y2, T2 can be smaller than X2 at the same time than Y2. In this way, the bread cannot be cooked at any time shorter than Y2. In this way, there may be at most two types of baked bread.
Code:
Int Toastxtoast: Bake ( Vector < Int > Undertoasted, Vector < Int > Overtoasted ){
Sort (undertoasted. Begin (), undertoasted. End ());
Sort (overtoasted. Begin (), overtoasted. End ());
If (Undertoasted [undertoasted. Size ()-1] <= overtoasted [0]) {
Return 1;
}
Else
If (Undertoasted [0]> = overtoasted [0]
| Undertoasted [undertoasted. Size ()-1]> = overtoasted [overtoasted. Size ()-1]) {
Return -1;
}
Else {
Return 2;
}
}
|
Question 3: Village Road
Description:
On a planet, there are many cities and rural areas. Rural areas are isolated from cities. Now you want to build roads to connect rural areas with cities. If a village is connected to a city, other villages can connect to the city indirectly. Given the coordinates of all cities and villages, the minimum length of the road to be built must be calculated. The distance between two points is the Euclidean distance.
Analysis:
If a is used to represent a city, B is used to represent a Village Connected to the city, and C is used to represent a village not connected to the city. For each village in C, the minimum length of the road to be built is either to a city in a or to a village in B. In this case, we should first establish a path to connect A or B to the nearest vertex x in C. If you first build y to another point in C, then the next X will be connected to the road network through point Z in C, which is much farther than directly connecting X to the road network. This is an obvious greedy nature. This means that every time we select a point X closest to the road network from C, we can add it to B. When all vertices in C are added to the road network, the calculation is over and the obtained road network is the shortest.
Code:
Double Kingdomxcitiesandw.agesanother: determinelength ( Vector < Int > Cityx,
Vector < Int > Cityy,
Vector < Int > Languagex,
Vector < Int > Languagey ){
Int Cities = cityx. Size ();
Int Ages = languagex. Size ();
Vector < Double > Distance (AGEs );
Vector < Bool > Flag (ages, False );
Int Count = 0;
Double Sum = 0;
For ( Int I = 0; I <cities; I ++ ){
For ( Int J = 0; j <ages; ++ J ){
Double D = DIS (( Double ) Cityx [I], ( Double ) Cityy [I], ( Double ) Villagex [J], ( Double ) Villagey [J]);
If (I ){
If (Distance [J]> d ){
Distance [J] = D;
}
}
Else {
Distance [J] = D;
}
}
}
Do {
Int K =-1;
Double Min =-1;
For ( Int I = 0; I <ages; I ++ ){
If (! Flag [I]) {
If (K <=-1 ){
K = I;
Min = distance [I];
}
Else {
If (Distance [I] <min ){
K = I;
Min = distance [I];
}
}
}
}
Flag [k] = True ;
Count ++;
Sum + = min;
For ( Int I = 0; I <ages; I ++ ){
If (! Flag [I]) {
Double D = DIS (( Double ) Villagex [I], ( Double ) Languagey [I], ( Double ) Villagex [K], ( Double ) Villagey [k]);
If (D <distance [I]) {
Distance [I] = D;
}
}
}
} While (Count <ages );
Return SUM;
}
|
Summary
In this case, the first and third questions are the easiest. For the second question, you only need to understand the question and find several key points. Sadly, I got stuck in the second question during the competition. I read the English question many times. I still have 20 minutes to understand it. In the past 20 minutes, I did not find that the maximum baking time must be greater than the maximum roasting time. I wrote only the code for returning-1 and 1, but a return 2 error.
It takes half an hour to watch the topcoder and 45 minutes to watch the topcoder each time. In 45 minutes, we need to discover the conditions contained in the question. It is still difficult for me to design an effective algorithm based on this. Cough: I still need to improve my English reading ability. I usually practice more.