Boxes in a Line UVA - 12657 

题目:UVA - 12657

这个题目用STL或者自己写一个链表TLE,只能用数组模拟,时间卡的很死,求和的那个我本来写函数里又TLE了,然后写主函数里A了....

TLE代码:
#include<iostream>
#include <stdio.h>
using namespacestd;
struct node {
    int data;
    node *pre;
    node *next;
};

node *creatnode(int m){
    node *head,*tail=NULL,*temp=NULL;
    head=new node;
    int i=0;
    head->data=i;
    i++;
    head->next=NULL;
    head->pre=NULL;
    tail=head;
    for(int I=0; I<m+1; I++) {
        temp=new node;
        temp->next=NULL;
        temp->pre=NULL;
        temp->data=i;
        i++;
        tail->next=temp;
        temp->pre=tail;
        tail=temp;
    }
    return head;
}
node *found(int n,node *head){
    while(head!=NULL) {
        if(head->data==n)
            return head;
        head=head->next;
    }
}
node *lift(int m,int n,node *head){
    node *a=found(m,head);
    node *b=found(n,head);
        a->pre->next=a->next;
        a->next->pre=a->pre;
        a->next=b;
        a->pre=b->pre;
        b->pre->next=a;
        b->pre=a;
    return head;

}
node *right(int m,int n,node *head){
    node *a=found(m,head);
    node *b=found(n,head);
        a->pre->next=a->next;
        a->next->pre=a->pre;
        a->pre=b;
        a->next=b->next;
        b->next->pre=a;
        b->next=a;
    return head;

}
node *change(int m,int n,node *head){
    node *a=found(m,head);
    node *b=found(n,head);
    node *c;
    c=new node;
    c->data=b->data;

        a->pre->next=c;
        a->next->pre=c;
        c->pre=a->pre;
        c->next=a->next;
        b->pre->next=a;
        b->next->pre=a;
        a->pre=b->pre;
        a->next=b->next;
        delete b;
    return head;
}
node *reverse(node *head){
    node *tail=head;
    head->pre=head->next;
    head->next=NULL;
    head=head->pre;
    while(head->next!=NULL) {
       head->pre=head->next;
       head->next=tail;
       tail=head;
       head=head->pre;
    }
    head->next=head->pre;
    head->pre=NULL;
    return head;
}
/*void show(node *head){ head=head->next; while(head->next!=NULL){ cout<<head->data<<" "; head=head->next; } cout<<endl;}*/
void getsum(int n,node *head){
    node *tail;
    long long sum=0;
    int i=1;
    tail=head;
    head=head->next;
    delete tail;
    while(head->next!=NULL){
       if(i%2!=0)
           sum+=head->data;
       tail=head;
       head=head->next;
       delete tail;
       i++;
    }
    cout<<"Case"<<" "<<n<<":" <<" "<<sum<<endl;
}
int main() {
    int m,n,t,a,b,k=0;
    node *head=NULL;
    while(scanf("%d",&m)!=EOF) {
        k++;
        head=creatnode(m);
        cin>>n;
        //show(head);
        for(int i=0;i<n;i++){
           cin>>t;
           if(t==1){
           cin>>a>>b;head=lift(a,b,head);
           }
           if(t==2){
           cin>>a>>b;head=right(a,b,head);
           }
           if(t==3){
           cin>>a>>b;head=change(a,b,head);
           }
           if(t==4)
           head=reverse(head);
        }
        //show(head);
        getsum(k,head);
    }
    return 0;
}



AC代码:
#include <iostream>
#include <cstdio>




using namespace std;
int l[100005],r[100005];




void Ini(int n) {
    int i;
    for(i=0; i<n+2; i++) {
        l[i]=i-1;
        r[i]=i+1;
    }
}




void exchange(int a,int b,int t) {
    if(t) {             //a放在b的左边
        r[l[a]]=r[a];
        l[r[a]]=l[a];
        r[l[b]]=a;
        l[a]=l[b];
        r[a]=b;
        l[b]=a;
    } else {           //a放在b的右边
        r[l[a]]=r[a];
        l[r[a]]=l[a];
        l[r[b]]=a;
        r[a]=r[b];
        r[b]=a;
        l[a]=b;
    }
}
long long getsumzeng(int n) {
    int i=0,k=r[0];
    long long sum=0;
    while(i!=n) {
        i++;
        if(i%2)
            sum+=k;
        k=r[k];
    }
    return sum;
}
long long getsumjian(int n) {
    int i=0,k=l[n+1];
    long long sum=0;
    while(i!=n) {
        i++;
        if(i%2)
            sum+=k;
        k=l[k];
    }
    return sum;
}
int main() {
    int N=0,n,m;
    while(~scanf("%d %d",&n,&m)) {
        int n,m,p=0;
        Ini(n);
        while(m--) {
            int a,b,k,t;
            scanf("%d",&k);
            if(k==1) {
                scanf("%d %d",&a,&b);
                exchange(a,b,k-p);
            } else if(k==2) {
                scanf("%d %d",&a,&b);
                exchange(a,b,!(k-p-1));
            } else if(k==3) {
                scanf("%d %d",&a,&b);
                if(l[a]==b)
                    exchange(a,b,1);
                else if(r[a]==b)
                    exchange(a,b,0);
                else {
                    t=l[a];
                    exchange(a,b,1);
                    exchange(b,t,0);
                }
            } else
                p=!p;
        }
        if(!p||n%2)
            printf("Case %d: %lld\n",++N,getsumzeng(n));
        else
            printf("Case %d: %lld\n",++N,getsumjian(n));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值