Poj 2454 (Greedy + randomization algorithm)

Source: Internet
Author: User

Problem description:

Known:Given an integer sequence s with a length of 3 K (1 ≤ k ≤ 60), it is divided into three sub-sequences S1, S2, and S3 with an equal length (K each, the addition of at least two subsequences must be greater than 500 * K.

Please:A division that meets the preceding conditions (the given input must have a solution)

Input:1st behavior K value; 2nd rows to 3rd k + 1 behavior sequence S Element value

Output:Number Division meeting conditions

Sample input:

2

510

500

500

500

670

400

310

Sample output:

1

2

3

6

5

4

Ideas:

First, as long as the sum of S1, S2, and S3 is greater than 500 * K, we adopt a greedy policy to sort the input sequence s in descending order, divide the first 2 k elements after sorting into S1 and S2 (assuming that S1 and S2 are the final conditions), and no longer consider S3

Set s' = (S1, S2). The randomization algorithm is now considered.

First strategy:Recyclically sorts all elements in S and performs condition detection. If conditions are met, the output results are exited. Otherwise, the loop continues. This strategy will cause a lot of waste, because the following situations occur: After a random rearrangement, the elements contained in S1 and S2 do not change, making this rescheduling meaningless. In addition, it takes a lot of time to rearrange the entire s '.

Second strategy:Each time, one element is randomly selected in S1 and S2 for interchange and condition detection. This policy ensures that most of the rearrangements are valid (unless the two elements selected randomly happen to be the two elements randomly selected last time)

Code (Second Policy):

# Include <stdio. h> # include <time. h> # include <stdlib. h> # include <limits. h> int A [200] [2]; int K; void sort () {int n = 3 * k; For (INT I = 0; I <n-1; I ++) {int max = I; for (Int J = I + 1; j <n; j ++) {if (a [J] [0]> A [Max] [0]) max = J;} If (max! = I) {int tmp1 = A [Max] [0]; int tmp2 = A [Max] [1]; A [Max] [0] = A [I] [0]; A [Max] [1] = A [I] [1]; A [I] [0] = tmp1; A [I] [1] = tmp2 ;}} int main () {scanf ("% d", & K ); int floor = 500 * k; For (INT I = 0; I <3 * k; I ++) {scanf ("% d ", & A [I] [0]); A [I] [1] = I;} Sort (); int sum1 = 0; int sum2 = 0; for (INT I = 0; I <K; I ++) {sum1 + = A [I] [0]; sum2 + = A [I + k] [0];} srand (unsigned INT) time (0); While (! (Sum1> floor & sum2> floor) {int offset1 = rand () % K; int offset2 = rand () % K; sum1 = sum1-a [offset1] [0] + A [K + offset2] [0]; // note that the first update and then swap element sum2 = sum2-a [K + offset2] [0] + A [offset1] [0]; int tmp1 = A [offset1] [0]; int tmp2 = A [offset1] [1]; A [offset1] [0] = A [K + offset2] [0]; A [offset1] [1] = A [K + offset2] [1]; A [K + offset2] [0] = tmp1; A [K + offset2] [1] = tmp2;} For (INT I = 0; I <3 * k; I ++) {printf ("% d \ n ", A [I] [1] + 1);} return 0 ;}

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.