"UVa 11134" fabled Rooks (algorithmic efficiency-problem decomposition + greed)

Source: Internet
Author: User

Test instructions: requires that n cars be placed on a n*n board, so that they are in different rows and columns, and are in the first I rectangle respectively.

Solution: problem decomposition + greed.

Because rows and columns are not relevant, it is possible to break down the problem of different rows and columns to 2 "Select n different integers in the interval [1,n] to make the I integer within [Li,ri]".

The next greedy is very important: first make the interval r from small to large, and then L. This will ensure that the right segment of each interval is flexible and legal if the number of the left is selected in each legal interval, and if R1=R2, it is not affected because it is filled from the left. Anyway, I didn't find a counter-example ... And not like me (???? ') ↓ said the same.

In fact, my thinking process is a kind of ... Think of the first sort of L, then R, to directly sift out the interval to the right of each interval and the next beginning repetition. As a result, when L1=L2 was first discovered, it was also filled from the left, then suddenly found that the length of 1 is "dangerous", [1,4], [1,4] and [2,2]. (? _ ?) So, should quickly change the idea! Just like sometimes the past greedy, but from the back to the greedy, instead of the first sort of r, and then L, this way to try some special data can be found.

╮ (╯_╰) ╭ Well, with the code, I don't use pointers, so the code is a lot longer.

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <algorithm>5#include <iostream>6 using namespacestd;7 #defineN 50108 9 intN;Ten intVis[n]; One structnode{intl,r,id,t;} A[n],b[n]; A intSx[n],sy[n]; -  - BOOLCMP (node X,node y) the { -     if(X.R!=Y.R)returnx.r<Y.R; -     returnx.l<Y.L; - } + BOOLSolve () - { +Sort (A +1, A +1+n,cmp), Amemset (Vis,0,sizeof(Vis)); at      for(intI=1; i<=n;i++) -     { -       BOOLtf=false; -        for(intj=a[i].l;j<=a[i].r;j++) -         if(!Vis[j]) -         { inA[i].t=j, vis[j]=1; -tf=true; Break; to         } +       if(!TF)returnTF; -     } theSort (b +1, B +1+n,cmp), *memset (Vis,0,sizeof(Vis)); $      for(intI=1; i<=n;i++)Panax Notoginseng     { -       BOOLtf=false; the        for(intj=b[i].l;j<=b[i].r;j++) +         if(!Vis[j]) A         { theB[i].t=j, vis[j]=1; +tf=true; Break; -         } $       if(!TF)returnTF; $     } -     return true; - } the intMain () - {Wuyi      while(1) the     { -scanf"%d",&n); Wu       if(!n) Break; -        for(intI=1; i<=n;i++) About       { $scanf"%d%d%d%d",&a[i].l,&b[i].l,&a[i].r,&B[I].R); -A[i].id=b[i].id=i; -       } -       if(!solve ()) {printf ("impossible\n");Continue;} A        for(intI=1; i<=n;i++) +sx[a[i].id]=a[i].t,sy[b[i].id]=b[i].t; the        for(intI=1; i<=n;i++) -printf"%d%d\n", Sx[i],sy[i]); $     } the     return 0; the}

"UVa 11134" fabled Rooks (algorithmic efficiency-problem decomposition + greed)

Related Article

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.