Example 6-5 Boxes in a line uVa12657

Source: Internet
Author: User

The solution to this problem is a doubly linked list, the data structure itself is not complex, but for four cases of processing is not detailed, mainly reflected in the following points:

    1. Classification discussion is not comprehensive, without considering special circumstances (itself does not need to operate, need to interchange two elements adjacent)
    2. No consideration of the effect on other operations after the change of State 4
    3. No flexible use of mathematical knowledge (courtship only needs to subtract all the odd numbers)

The following post the AC code

#include <cstdio>
#include <algorithm>
const int MAXN = 100000 + 10;
int LEFT[MAXN];
int RIGHT[MAXN];
int S[MAXN];
using namespace Std;
void link (int x,int y) {
Right[x] = y;
Left[y] = x;
}
int main () {
#ifdef DEBUG
Freopen ("6.5.in", "R", stdin);
#endif
int n, m, num = 0;
while (scanf ("%d%d", &n, &m) ==2) {
for (int i = 1; I <= n; i++) {
Right[i]= (i+1);
Left[i]=i-1;
}
Right[0]=1;
Left[0]=n;
int op,x,y;
int INV = 0;
while (m--) {
scanf ("%d", &op);
if (op = = 4) Inv=!inv;
else {
scanf ("%d%d", &x, &y);
if (op = = 3 && right[y] = = X) swap (x, y);
if (OP! = 3 && Inv) op = 3-op;
if (op = = 1 && X = = Left[y]) continue;
if (op = = 2 && X = = Right[y]) continue;

int lx= Left[x],rx = right[x],ly = Left[y], RY = Right[y];
if (op = = 1) {
Link (lx,rx); link (ly,x); link (x, y);
}
else if (op = = 2) {
Link (lx,rx); link (y,x); link (x,ry);
}
else if (op = = 3) {
if (right[x] = = Y) {
Link (LX, y); Link (y, X); Link (X, RY);
}
else{
Link (LX, y); Link (y, RX); Link (LY, X); link (x,ry);
}
}
}
}
A long long ans = 0;
int j=0;
for (int i = 1;i<=n; i++) {
J=RIGHT[J];
if (i% 2 = = 1) ans+=j;
}
if (inv && n%2 = = 0) ans = (long Long) n * (n+1)/2-ans;
printf ("Case%d:%lld\n", ++num, ans);
}
return 0;
}

Example 6-5 Boxes in a line uVa12657

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.