Poj 1042 (Greedy)

Source: Internet
Author: User
Http://poj.org/problem? Id = 1042

The first question made during the winter vacation was definitely abused .. Lili's wa had no more than 20 times, and finally there was no way to let others find out the error! Sad reminder...

 

This question is greedy, enumerative to the end of the island I, and the time spent on the island is reduced from the total time. Then greedy for the maximum value in I Island.

There are two errors:

1. The question requires extra time to be placed on the first Island. I added cur = 0 to the selected max, and added the time directly to the first Island.

In fact, this does not need to be processed separately. cur = 0 has been made before greed. If max <0. Fish, cur will not change. One click

2. The order of resetting and decreasing when the number of fish is negative. After clearing, I make cur = 0 and then decrease f [0.

In this case, the operation can be negative for the fish, but it cannot be done if the fish is negative after the decrease, which may cause the island selection error in the greedy process.

 

# Include <iostream>
# Include <fstream>
# Include <cstring>
# Include <cstdlib>
# Define max (A, B) A> B? A: B
Using namespace STD;

Struct f {
Int fish;
Int DESC;
Int walk;
};
F [30];
F ftemp [30];
Int time [30] [30];
Int sum [30];

Int main (){
Int N, H, total, I, j, Max, maxf, cur, temp;
// Fstream CIN ("in. dat", IOS: In );
While (~ Scanf ("% d", & N ){
Scanf ("% d", & H );
For (I = 0; I <n; I ++)
Scanf ("% d", & F [I]. Fish );
For (I = 0; I <n; I ++)
Scanf ("% d", & F [I]. DESC );
For (I = 1; I <n; I ++)
Scanf ("% d", & F [I]. Walk );
Total = 12 * h;
F [0]. Walk = 0;
Memset (time, 0, sizeof (time ));
Memset (sum, 0, sizeof (SUM ));
For (I = 0; I <n; I ++) {// enumeration ends at Lake I
Total-= f [I]. Walk;
Temp = total;
Memcpy (ftemp, F, (I + 1) * sizeof (f ));
While (temp> 0) {// greedy
Maxf = ftemp [0]. fish;
Cur = 0; // each time the greedy start mark the position cur = 0 ensures that the remaining time belongs
For (j = 1; j <= I; j ++ ){
If (maxf <ftemp [J]. Fish ){
Maxf = ftemp [J]. fish;
Cur = J;
}
}
Sum [I] + = ftemp [cur]. fish;
Ftemp [cur]. Fish-= ftemp [cur]. DESC;
// Here we need to reduce the number before solving the non-positive situation.
If (ftemp [cur]. Fish <0)
Ftemp [cur]. Fish = 0;
Time [I] [cur] ++;
Temp --;
}
}
Max =-1;
Cur = 0;
For (I = 0; I <n; I ++ ){
If (sum [I]> MAX ){
Max = sum [I];
Cur = I;
}
}
Cout <time [cur] [0] * 5;
For (I = 1; I <n; I ++)
Cout <"," <time [cur] [I] * 5;
Cout <Endl <"number of fish expected:" <sum [cur] <Endl;
}

}

 

Error code block:

Maxf = ftemp [0]. fish;
Cur = 0;
For (j = 1; j <= I; j ++ ){
If (maxf <ftemp [J]. Fish ){
Maxf = ftemp [J]. fish;
Cur = J;
}
}
If (ftemp [cur]. Fish <= 0 ){
Ftemp [cur]. Fish = 0;
Cur = 0;
}
Sum [I] + = ftemp [cur]. fish;
Ftemp [cur]. Fish-= ftemp [cur]. DESC;
Time [I] [cur] ++;

Temp --;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.