UVA 12657 Boxes in a line

Source: Internet
Author: User

Topic Link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=4395




The title means, given a number n, that there is an integer sequence of 1......N, followed by four operations:

Action One: Enter X, y, which means to move the X. to the left of Y (if x is omitted on the left side of y);

Operation two: Enter X, y, which means to move X. to the right of Y (if x is omitted on the right side of y);

Action Three: Enter X, Y, which represents the interchange of X. and Y.

Operation Four: Invert the entire sequence.

The last requirement is the number of odd items of the entire sequence after the operation.

The data is 100000, the direct simulation is definitely timed out, and the list with STL is super, my idea is to use the array to simulate the list, left[i] and right[i] arrays to hold the left and right values of I respectively. In the whole process, reverse operation the most time-consuming, because the topic only requires odd number of and, in the reverse operation, we do not need to really do the operation, only need to record the number of reverse order, if odd several times, then the last of the entire sequence of even items of the sum, even several times is to find the sum of odd items. Another point is that whether or not the operation four will have a little effect on the first two operations, need to be handled slightly. See the code for the detailed procedure:


#include <algorithm>//#include <iostream>//cannot be used here iostream, and Right,left conflict, #include <cstring># Include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <cmath># Include <ctime> #include <queue> #include <stack> #include <set> #include <map>using       Namespace Std;typedef long long ll;const int maxn= 100000 + 10;int left[maxn],right[maxn];void link (int L, int R) {    Connect L and R,l on left right[l] = R; Left[r] = L;}    int main () {int n,m,cas=1;        while (~SCANF ("%d%d", &n,&m)) {for (int i=0; i<=n; i++) {link (i,i+1);        } left[0]=n;        right[n]=0;        int op,flag=0,x,y;            while (m--) {scanf ("%d", &op);                if (op==4) {//To determine whether to reverse the operation Flag=!flag;           Continue Note: When op==4, you do not need to enter X, Y. (This is a long time finding the wrong place ...%>_<% ...            )} scanf ("%d%d", &x,&y); if (op==3 &&Amp  RIGHT[Y]==X) Swap (x, y);          Convenient back to determine if the exchange is adjacent if (op!=3 && flag) Op=3-op;            After the reverse operation, the original left turns to the right, and the right turns to the left if (op==1 && left[y]==x) continue;            if (op==2 && right[y]==x) continue;            int lx=left[x],rx=right[x],ly=left[y],ry=right[y];                if (op==1) {//move X to the left of Y Link (lx,rx);                Link (ly,x);            Link (x, y);                } else if (op==2) {//moves x to the right of Y link (lx,rx);                Link (y,x);            Link (x,ry);                    } else if (op==3) {//interchange X and Y if (right[x]==y) {//when x and y are adjacent to link (lx,y);                    Link (y,x);                Link (x,ry);                    } else {//when x and Y are not adjacent, link (lx,y);                    Link (Y,RX);                    Link (ly,x);                Link (x,ry);        }}} LL sum=0;      int b=0;  for (int i=1; i<=n; i++) {b=right[b];        if (i%2) sum+=b;        } if (flag && n%2==0) sum= (LL) n (n+1)/2-sum;        If the number of reverse operations is odd, the final result is the sum of the even items of the entire sequence, where the sums of the sequence are subtracted from the odd items and printf ("Case%d:%lld\n", cas++,sum); Here the output is too pit, because CB only support __int64, does not support%LLD, and HUSTOJ support Longlong does not support Int64,wa several times} return 0;}


/* Sample Input:6 4  1 1 4  2 3 5  3 1 6  4  6 3  1 1 4  2 3 5  3 1 6  100000 1  4Sample Output:case 1:12 Case  2:9 case  3:2500050000*/


Unfortunately this problem in the game thinking is right, due to various issues considered not all did not do it ... Fortunately, it's just a regular practice game.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 12657 Boxes in a line

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.