elysia_nice 2022-10-23 11:47 采纳率: 80%
浏览 11
已结题

uva 12657 解法超时了,求方法提高速度

img

img


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include<string>
#include<iostream>
#include<list>
using namespace std;
int main() {
    int n = 0, m = 0;
    while (scanf("%d%d",&n,&m) == 2) {
        list<long long int>one;
        for (long long int i = 1; i <= n; i++) {
            one.push_back(i);
        }
        for (long long int j = 1; j <= m; j++) {
            long long int t = 0, k = 0, q = 0;
            long long int x = 0, y = 0;
            list<long long int>::iterator fr, sc;
            scanf("%d", &t);
            switch (t){
            case 1:
            case 2:
            case 3:
                scanf("%d%d", &k, &q); if (k == q)continue;
                for (fr = one.begin(); *fr != k; fr++, x++);
                for (sc = one.begin(); *sc != q; sc++, y++);
            switch (t) {
            case 1:
                if (x==y-1) continue;
                else { one.insert(sc, *fr); one.erase(fr); }
                break;
            case 2:
                if (x==y+1) continue;/*insert的本质*/
                else { one.insert(++sc, *fr); one.erase(fr); }
                break;
            case 3:
                swap(*fr, *sc);
                break;
            }
                break;
            case 4:
                one.reverse();
                break;
            default:
                continue;
            }
        }
        long long int  sum = 0, k = 1;
        list<long long int>::iterator it;
        for (it=one.begin(); it!=one.end(); it++, k++)if (k % 2 != 0)sum += *it;
        ::cout << sum;
        ::cout << "\n";
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<list>
using namespace std;
int main() {
    int n = 0, m = 0;
    while (scanf("%d%d",&n,&m) == 2) {
        long long int num[100005] = { 0 };
        for (long long int i = 1; i <= n; i++) {
            num[i - 1] = i;
        }
        list<long long int>one(num,num+n);
        for (long long int j = 1; j <= m; j++) {
            int t = 0, k = 0, q = 0;
            int x = 0, y = 0;
            list<long long int>::iterator fr, sc;
            scanf("%d", &t);
            switch (t){
            case 1:
            case 2:
            case 3:
                scanf("%d%d", &k, &q); if (k == q)continue;
                for (fr = one.begin(); *fr != k; fr++, x++);
                for (sc = one.begin(); *sc != q; sc++, y++);
            switch (t) {
            case 1:
                if (x==y-1) continue;
                else { one.insert(sc, *fr); one.erase(fr); }
                break;
            case 2:
                if (x==y+1) continue;/*insert的本质*/
                else { one.insert(++sc, *fr); one.erase(fr); }
                break;
            case 3:
                swap(*fr, *sc);
                break;
            }
                break;
            case 4:
                one.reverse();
                break;
            default:
                continue;
            }
        }
        long long int  sum = 0, k = 1;
        list<long long int>::iterator it;
        for (it=one.begin(); it!=one.end(); it++, k++)if (k % 2 != 0)sum += *it;
        ::cout << sum;
        ::cout << "\n";
    }
    return 0;
}


  • 写回答

1条回答 默认 最新

  • .LAL. C/C++领域新星创作者 2022-10-23 17:55
    关注
    
    #include<bits/stdc++.h>
    using namespace std;
    int r[100005],l[100005];
    void link(int a,int b)
    {
        r[a]=b;l[b]=a;
    }
    int main()
    {
        int n,m,op,x,y,flag=0,cas=0;
        while(scanf("%d %d",&n,&m)==2)
        {
            for(int i=1;i<=n;i++)
            {
                r[i]=(i+1)%(n+1);
                l[i]=i-1;
            }
            r[0]=1;
            l[0]=n;
            flag=0;
            while(m--)
            {
                scanf("%d",&op);
                if(op!=4)
                scanf("%d %d ",&x,&y);
                else
                flag=!flag;
                if(op==3&&r[x]==y)
                swap(x,y);
                if(op<3&&flag)
                op=3-op;
                if(op==1&&l[y]==x)
                continue;
                if(op==2&&r[y]==x)
                continue;
                int rx=r[x],lx=l[x],ry=r[y],ly=l[y];
                if(op==1)
                {
                    link(lx,rx);
                    link(ly,x);
                    link(x,y);
                }
                if(op==2)
                {
                    link(lx,rx);
                    link(y,x);
                    link(x,ry);
                }
                if(op==3)
                {
                    if(l[x]==y)
                    {
                        link(ly,x);
                        link(x,y);
                        link(y,rx);
                    }
                    else
                    {
                        link(lx,y);
                        link(y,rx);
                        link(ly,x);
                        link(x,ry);
                    }
                }
            }
            int k=0;
            long long ans=0;
            for(int i=1;i<=n;i++)
            {
                k=r[k];
                if(i%2==1)
                ans+=k;
            }
            //cout<<(long long)n/2*(n+1)<<endl;
            if(flag&&n%2==0)
                ans=(long long)n/2*(n+1)-ans;
            printf("Case %d: %lld\n",++cas,ans);
        }
        return 0;
     } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月19日
  • 已采纳回答 11月11日
  • 创建了问题 10月23日

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗