Bzoj 3519: [Zjoi2014] elimination of chess pieces

Source: Internet
Author: User

"Preface" In the eyes of suspicion, I made a noon and half an afternoon, a night of the topic finally a!

"Original question"

The elimination piece is an interesting game. The game is on a board of R * C. Every lattice of a chessboard

Child It is either empty or a piece of color. There are exactly two pieces of the same color.

Every round,
The player is able to select an empty lattice (x, y) and select two directions in the upper and lower left and right four directions, assuming
There are checkers in all two directions, and the first piece to be encountered along these two directions
The same color, then, we will take these two pieces away. and call it a legitimate operation. Otherwise, call this a screw.
Is illegal, the game will not handle this operation. The aim of the game is to eliminate as many pieces as possible.


Give a game and a person's play. You need to:
Z points out how many pieces the person can eliminate.
Z gives a scheme that can eliminate the most pieces.
"Input Format"
In the input file eliminate.in, the first line gives the integer r, c.

The second line gives the integer n.
Indicates the number of different colors. Next n rows, line I contains 4 integers a[i], b[i], c[i], d[i]. Representing colors
For the two lattices of I are respectively (A[i], b[i]), (C[i], d[i]).

Then there is an integer m. Indicates that this person's exercise
Count The next M-line, with 2 integers and 2 letters per line, represents the lattice of his choice, and
Two directions. We use "UDLR" to represent the top and bottom of each other.
"Output Format"
In the output file Eliminate.out, the first line outputs how many pieces the person can eliminate.

The second row contains a

6


"example input"  
4 4 
4 
1 1 1 4 
1 2 3 4 
1 3 3 2 
4 1 2 3 
6 
2 3 U r 
2 1 D r 
2 2 L r 
2 4 l d 
3 1 L r 
3 3 L u 

Example

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamlhbmdzagliawfv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

"Analysis" started to feel the idea is quite simple, but the code is really written more and more long ...

My basic idea is to use set to maintain the adjacent situation, a bit like the garden that I did the other day. A set is opened for each row and each vertical column, indicating the coordinates on this row and vertical column. Each operation is based on the lower_bound.

First consider the person who is reading.

Obviously this is very easy (Code 23 line), direct simulation can be. For example, we want to eliminate such a pair of pieces.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamlhbmdzagliawfv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Set the upper left corner (x1,y1), lower right corner (X2,Y2). We can delete the Y1 in the set where the X1 is located, delete the X1 in the set where Y1 is located, and so on X2,y2. Each time you infer whether there are pieces on the middle route of a pair of points and delete points.

The trouble is the optimal value, the basic trouble lies in the classification discussion.

First put the direct can delete into the queue, and then each time to take out a pair of points. Delete this pair of points first. Then infer if there are any pairs of points near this point that can be deleted as a result of deleting this pair of points, and the same time the queue.

Take a look at some details.

① may be in the queue several times, so use flag to record it.

② Don't forget the case of x1=x2 and Y1=y2.

③ clearly enumerates the first points (x1,y1) and (X2,y2) four weeks past. But attention is just not enough, there are (X1,y2) and (x2,y1).

④ this point is the most pit! Attention. Lower_bound does not really return to the end (), but in erase, find, and so on, assuming that there is no special sentence to bring in the iterator or value is incorrect, will explode. I've been re since I just finished writing.

Then I started a long journey of tuning the program. It has been a bit forgotten where the error has occurred, but anyway the program is getting longer (although the number of rows is not very much).

And then, from 1 points to 3 points to 5 points. And then it's all big data.

So he started to shoot. For convenience, I only take the best value case.

"Build Data Program"

#include <ctime> #include <cstdlib> #include <cstdio>using namespace Std;bool f[40005][40005];int x1, Y1,x2,y2;int Main () {  freopen ("Eliminate.in", "w", stdout);  Srand ((int) time (0));  int r=10,c=10;  printf ("%d%d\n", r,c);  int n=5;  printf ("%d\n", n);  for (int i=1;i<=n;i++)  {while    (1)    {      X1=rand ()%r+1;y1=rand ()%c+1;x2=rand ()%r+1;y2=rand ()%c +1;      if (!f[x1][y1]&&!f[x2][y2]&& (x1!=x2| | Y1!=Y2)) break;    }    printf ("%d%d%d%d\n", x1,y1,x2,y2);    f[x1][y1]=f[x2][y2]=1;}  }

I really want to thank my data-making program. The first is the discovery of the x1==x2 when a bit of a problem. After that, there were 8 points. Then the remaining two biggest points have been wrong, and the answer is only a little bit different!! Then I just good again helpless to Pat, about 20 minutes after the n==5 error.

Here is the wrong point picture:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvamlhbmdzagliawfv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "> later found that the iterator pointer basically every function has, also opened the global variable. In this way, the iterator in find is slightly changed by running a check process. The data is really awesome!

Code

#include <cstdio> #include <cstring> #include <set> #include <map> #define N 100005using Namespace Std;map<int,int>prex[n],prey[n];multiset<int>p[n],q[n],x[n],y[n];typedef Multiset<int >::iterator it;it it1,it2,it;int M,n,r,c,h,t,x,y,x1,x2,y1,y2,i,wri[n][2],id[n][4],flag[n];char re[N][2];void  Person () {scanf ("%d", &m), int ans=0,c[2];char s1[2],s2[2];it it[2];    while (m--) {scanf ("%d%d%s%s", &AMP;X,&AMP;Y,&AMP;S1,&AMP;S2);    if (P[x].find (y)!=p[x].end ()) continue;    int O=0,tx,ty;it it[2];c[0]=-1;c[1]=-2; if (s1[0]== ' U ' | |    s2[0]== ' U ') {it[o]=q[y].lower_bound (x); if (It[o]==q[y].begin ()) continue;c[o]=prey[y][* (--it[o])];o++;} if (s1[0]== ' L ' | |    s2[0]== ' L ') {it[o]=p[x].lower_bound (y); if (It[o]==p[x].begin ()) continue;c[o]=prex[x][* (--it[o])];o++;} if (s1[0]== ' D ' | |    s2[0]== ' D ') {it[o]=q[y].lower_bound (x); if (It[o]==q[y].end ()) continue;c[o]=prey[y][*it[o]];o++;} if (s1[0]== ' R ' | | s2[0]== ' R ') {it[o]=p[x].lower_bound (y); if (It[o]==p[x].end ()) continue;c[o]=prex[x][*it[o]];o++;}    if (c[0]!=c[1]) continue;    ans+=1;o=0; if (s1[0]== ' U ' | | s2[0]== ' U ') {Tx=*it[o]; Q[y].erase (It[o]);    P[tx].erase (P[tx].find (y)); o++;} if (s1[0]== ' L ' | | s2[0]== ' L ') {Ty=*it[o]; P[x].erase (It[o]);    Q[ty].erase (Q[ty].find (x)); o++;} if (s1[0]== ' D ' | | s2[0]== ' D ') {Tx=*it[o]; Q[y].erase (It[o]);    P[tx].erase (P[tx].find (y)); o++;} if (s1[0]== ' R ' | | s2[0]== ' R ') {Ty=*it[o]; P[x].erase (It[o]);  Q[ty].erase (Q[ty].find (x)); o++;} } printf ("%d", ans);}  void y1_s_y2 (int num) {if (Flag[num]) return;  It1=x[x1].upper_bound (y1); It2=y[y2].lower_bound (x1); if (*it1>y2| |  It1==x[x1].end ()) &&*it2==x2) {re[++t][0]= ' L '; re[t][1]= ' D '; wri[t][0]=x1;wri[t][1]=y2;flag[num]=1;return;}  It1=y[y1].upper_bound (x1); It2=x[x2].lower_bound (y1); if (*it1>x2| | It1==y[y1].end ()) &&*it2==y2) {re[++t][0]= ' R '; re[t][1]= ' U '; wri[t][0]=x2;wri[t][1]=y1;flag[num]=1;}}  void y1_b_y2 (int num) {if (Flag[num]) return;  It1=y[y1].upper_bound (x1); It2=x[x2].upper_bound (y2); if ((*it1>x2| | It1==y[y1].end ()) && (*it2>y1| |  It2==x[x2].end ())) {re[++t][0]= ' L '; re[t][1]= ' U '; wri[t][0]=x2;wri[t][1]=y1;flag[num]=1;return;}  It1=x[x1].lower_bound (y2);  It2=y[y2].lower_bound (x1); if (*it1==y1&&*it2==x2) {re[++t][0]= ' R '; re[t][1]= ' D '; wri[t][0]=x1;wri[t][1]=y2;flag[num]=1;}}    void x1_e_x2 (int num) {if (Flag[num]) return;  if (y1>y2) swap (y1,y2); It it=x[x1].upper_bound (y1); if ((*it) <y2) return; re[++t][0]= ' L '; re[t][1]= ' R '; wri[t][0]=x1;wri[t][1]= (Y1+y2)/2;flag[num]=1;}  void y1_e_y2 (int num) {if (Flag[num]) return;  It It=y[y1].upper_bound (x1); if ((*it) <x2) return; re[++t][0]= ' U '; re[t][1]= ' D '; wri[t][0]= (x1+x2)/2;wri[t][1]=y1;flag[num]=1;}  void check (int x,int y) {x1=x;y1=y;int num=prex[x1][y1];  if (id[num][0]==x1&&id[num][1]==y1) x2=id[num][2],y2=id[num][3];  else x2=id[num][0],y2=id[num][1];  if (ABS (X1-X2) +abs (y1-y2) ==1) return;  if (x1>x2) swap (X1,X2), swap (y1,y2); int o=0;  if (x1==x2) {x1_e_x2 (num); return;} if (y1==y2) y1_e_y2 (nUM);  if (y1<y2) y1_s_y2 (num); if (y1>y2) y1_b_y2 (num);}    void do (int x,int y) {It dd=y[y].lower_bound (x); int fd=dd==y[y].end ();    It Rr=x[x].lower_bound (y); int fr=rr==x[x].end ();    It uu=dd;int fu= (Uu==y[y].begin ()); if (!fu) uu--;    It ll=rr;int fl= (Ll==x[x].begin ()); if (!FL) ll--;    int d=*dd,r=*rr,u=*uu,l=*ll;    if (!FD) check (d,y);    if (!FU) check (u,y);    if (!FL) check (x,l); if (!FR) check (x,r);}  void Find () {int g1,g2;     for (int i=1;i<=n;i++) {x1=id[i][0];     Y1=ID[I][1];  Check (X1,Y1);    } while (h<t) {int x=wri[++h][0],y=wri[h][1],tx,ty;      if (re[h][0]== ' U ' &&re[h][1]== ' D ') {y[y].erase (G2=*y[y].lower_bound (x));      Y[y].erase (g1=* (--y[y].lower_bound (x)));      X[g1].erase (X[g1].find (y));      X[g2].erase (X[g2].find (y));      It=y[y].lower_bound (x), if (It!=y[y].end ()) check (*it,y), if (It!=y[y].begin ()) Check (* (--it), Y);      It=x[g1].lower_bound (y); if (It!=x[g1].end ()) check (g1,*it); if (It!=x[g1].begin ()) Check (G1,* (--it));      It=x[g2].lower_bound (y); if (It!=x[g2].end ()) check (g2,*it);      if (It!=x[g2].begin ()) Check (g2,* (--it));    Continue      } if (re[h][0]== ' L ' &&re[h][1]== ' R ') {x[x].erase (G2=*x[x].lower_bound (y));      X[x].erase (g1=* (--x[x].lower_bound (y)));      Y[g1].erase (Y[g1].find (x));      Y[g2].erase (Y[g2].find (x));      It=x[x].lower_bound (y), if (It!=x[x].end ()) check (x,*it), if (It!=x[x].begin ()) Check (x,* (--it));      It=y[g1].lower_bound (x); if (It!=y[g1].end ()) check (*IT,G1);      if (It!=y[g1].begin ()) Check (* (--it), G1);      It=y[g2].lower_bound (x);      int t1=*it;it itt=it;int t2=* (--itt);      if (It!=y[g2].end ()) check (*IT,G2);      if (It!=y[g2].begin ()) Check (* (--it), G2);    Continue } if (re[h][0]== ' U ' | |    re[h][1]== ' U ') it=--y[y].lower_bound (x), Tx=*it,y[y].erase (IT), X[tx].erase (X[tx].find (Y)); if (re[h][0]== ' D ' | |    re[h][1]== ' D ') it=y[y].lower_bound (x), Tx=*it,y[y].erase (IT), X[tx].erase (X[tx].find (Y)); if (re[h][0]== ' L ' | | re[h][1]== ' L ') it=--x[x].lower_bound (y), ty=*it,x[x].erase (IT), Y[ty].erase (Y[ty].find (X)); if (re[h][0]== ' R ' | |    re[h][1]== ' R ') it=x[x].lower_bound (y), ty=*it,x[x].erase (IT), Y[ty].erase (Y[ty].find (X));  Do (x, y);D O (tx,ty);D O (x,ty);D O (tx,y); } printf ("%d\n", t);}  int main () {freopen ("eliminate.in", "R", stdin);  Freopen ("Eliminate.out", "w", stdout);  scanf ("%d%d", &r,&c);  scanf ("%d", &n); h=0;t=0;    for (i=1;i<=n;i++) {scanf ("%d%d", &x,&y);p rex[x][y]=prey[y][x]=i;id[i][0]=x;id[i][1]=y; P[x].insert (y);    Q[y].insert (x);    scanf ("%d%d", &x,&y);p rex[x][y]=prey[y][x]=i;id[i][2]=x;id[i][3]=y; P[x].insert (y);  Q[y].insert (x);  } for (i=1;i<=r;i++) x[i]=p[i];  for (i=1;i<=c;i++) y[i]=q[i];  Find (); return 0;}

Bzoj 3519: [Zjoi2014] elimination of chess pieces

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.