ZOJ3798 Abs problem

Source: Internet
Author: User

Title: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798

learned about the sequence with next_permutation violence to hit the table

You can find the pattern first.

#include <cstdio>#include<algorithm>#defineINF 0x3f3f3f3f//#defineN 100using namespacestd;intSeq[n];//Save current sequenceintMi[n], ma[n];//save minimum maximum value corresponding sequenceintCalcintN//Total Sort Number{    intp =1;  for(inti =1; I <= N; ++i) P*=i; returnp;}intMain () {intN;  while(~SCANF ("%d", &N)) { for(inti =1; I <= N; ++i) seq[i]=i; intk = Calc (n), MIN = INF, MAX =-INF;  for(inti =0; I < K; ++i) {intb; Next_permutation (Seq+1, seq + n +1);  for(inti =1; I <= N; ++i) {if(i = =1) B =Seq[i]; Elseb = ABS (b-Seq[i]); }            if(b <=MIN) {MIN=b;  for(inti =1; I <= N; ++i) mi[i]=Seq[i]; }            if(b>=MAX) {MAX=b;  for(inti =1; I <= N; ++i) ma[i]=Seq[i]; }} printf ("%d%d\n", MIN, MAX);  for(intj =1; J <= N; ++j) printf ("%d", Mi[j]); Puts ("");  for(intj =1; J <= N; ++j) printf ("%d", Ma[j]); Puts (""); }     return 0;}

Rule: The smallest in reverse order, the biggest in reverse

N%4==3 or 0 o'clock, the minimum value is 0 and the rest is 1

As an example: Sequence 7 6 5 4 3 2 1

At this point the sequence is the minimum value, it will be found that: starting from 7 per 4 number of absolute value is 0, every two number is 1, so if n%4==3, the remaining last 3 is 3 2 1, easy to get the minimum value of 0,n%4==0, 1, 2 o'clock the same.

Max value Max (n) =n-min (n-1)

AC Code:

1#include <cstdio>2 #defineN 500053 intGetmin (intN)4 {5     if(n%4==3|| N%4==0)6         return 0;7     return 1;8 }9 intMain ()Ten { One     intN; A      while(~SCANF ("%d", &N)) -     { -printf"%d%d\n", Getmin (n), N-getmin (N-1)); the          for(inti = n; i >0; --i) -         { -             if(I >1) printf ("%d", i); -             Elseprintf"%d\n", i); +         } -          for(inti = n1; i >0; --i) +printf"%d", i); Aprintf"%d\n", n); at     } -     return 0; -}

ZOJ3798 Abs problem

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.