1027 Ignatius and the princess II

Source: Internet
Author: User

/* 1027 Ignatius and the princess II */<br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <stdlib. h> </P> <p> int arr [9]; <br/> int Q [1, 1005]; <br/> int n, m; </P> <p> void Init () <br/>{< br/> int I; <br/> int res; <br/> for (I = 1, res = 1; I <= 7; I ++) {<br/> res * = I; <br/> arr [I] = res; <br/>}</P> <p> int find_min (int K) <br/>{< br/> int min = n + 1; <br/> int I; <br/> for (I = k + 1; I <= N; I ++) <br/>{< br/> If (Q [I] <q [Min] & Q [I]> q [k]) <br/> min = I; <br/>}< br/> return min; <br/>}</P> <p> void print () <br/>{< br/> int I; <br/> printf ("% d", Q [1]); <br/> for (I = 2; I <= N; I ++) <br/> printf ("% d", Q [I]); <br/> printf ("/N "); <br/>}</P> <p> int comp (const void * a, const void * B) <br/>{< br/> return * (int *) a-* (int *) B; <br/>}</P> <p> int comp1 (const void * a, const void * B) <br/>{< br/> return * (int *) B-* (int *) A; <br/>} </P> <p> int main () <br/> {<br/> int I, j; <br/> int min, sum; <br/> int temp; <br/> Init (); <br/> while (scanf ("% d", & N, & M) = 2) {<br/> for (I = 0; I <= n + 1; I ++) <br/> q [I] = I; <br/> for (I = 7, sum = 0; I> 0; I --) {<br/> sum + = arr [I]; <br/> If (sum <m) {<br/> min = find_min (n-I ); <br/> q [Min] ^ = Q [n-I]; <br/> q [n-I] ^ = Q [Min]; <br/> q [Min] ^ = Q [n-I]; <br/> qsort (& Q [n-I + 1], I, sizeof (INT ), comp); <br/> I ++; <br/>}< br/> Else if (sum> m) sum-= arr [I]; <br/> If (sum = m) {<br/> qsort (& Q [n-I + 1], I, sizeof (INT), comp1); <br/> Print (); <br/> break; <br/>}< br/> return 0; <br/>}</P> <p> // I am excited. It may seem an idiot to Daniel! However, you didn't know Baidu at all, and you didn't ask anyone else. You came up with a method that ranked 73 by time! <Br/> // I haven't tried this for a long time, because I have been doing new things all the time, in addition, I personally seldom think of a <br/> // method to solve the problem. Most of them are based on others' achievements ~~~~~ Next let's talk about my method ^-^ <br/> // The maximum value of question m is 10000, not very large <br/> // I thought about it, the sequence of N numbers is in full arrangement, that is, N !. <Br/> // I save 1-N to an array Q [], which is also the smallest one. <Br/> // the first small sequence of the question is to place as small as possible in front of the question, therefore, if necessary, do not change the previous number <br/> // that is, the sequence should be changed from the next 1.1 points. If M is 1, do not change the sequence, if M is 2, we need to reverse the reciprocal of two numbers <br/> // we still assume there are SEQUENCES {1, 2, 4, 5, 6} <br/> // M = 1: {1, 2, 3, 4, 5, 6} <br/> // M = 2: {1, 3, 4, 6, 5} <br/> // M = 3: {1, 2, 3, 5, 6, 4}. When M is 3, only the two reciprocal numbers cannot be changed. Then, when m is 3, it cannot be considered as 10? <Br/> // it refers to the full arrangement when we want to move the number of reciprocal I, that is, I! <M, we have to change the number of I-1. <Br/> // Well, the problem is clear now. <Br/> // now let's consider the worst case, that is, we may need to change all N numbers. Then M = n! (Note that the maximum question m is 10000 <8 !) <Br/> // if n!> M, that is, we do not need to change the number of all N, we also consider that we may want to change the number of N-1, that is, the number (n-1 )! The number of sequences is exactly the MB <br/> // But here M has two possible conditions <br/> // A). (n-1 )! = M. Then, the sequence to be searched is the maximum (least) sequence of the number of N-1 (not required in the previous number) in the reciprocal, that is to say, we need to put the n-1 numbers as big as possible before <br/> // number. This is where we sort the n-1 numbers from large to small, output the data with the preceding number. <Br/> // B). (n-1 )! <M <n !, At this time, the number of the last n also needs to change, so m, because we should try to put the number on the front, so we can find the smallest one from the number of N-1 in the last column and change the number of <br/> // nth in the last column, and then sort the number of N-1 in the last column from small to large. In this way, the number of 1st sequences is determined, and the number of N-1 records to be counted down is unknown. <br/> // here we apply the variable sum = (n-1 )! Write down the current sequence as the number of sequences, and then we consider the number of reciprocal (n-2), So repeat until when sum = m put) replace 1 with I <br/> // is actually a result of a certain number.

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.