首页 > 程序开发 > 综合编程 > 其他综合 >

UVa12657

2016-11-22

UVa12657

#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include 
#include 
#include
#include
#include
#include
#include

using namespace std;
/*
const int maxn = 100000 + 5;
int L[maxn], R[maxn];

void link(int a,int b)
{
    R[a] = b;
    L[b] = a;
}

int vs_main()
{
    int n, m;
    int cas = 1;
    while (cin >> n >> m)
    {
        for (int i = 1;i <= n;i++)
        {
            L[i] = i - 1;
            R[i] = (i + 1) % (n + 1);
        }
        L[0] = n;
        R[0] = 1;
        int a, b, op;
        int flag = 0;

        for (int i = 0;i < m;i++)
        {
            cin >> op;
            if (op == 4)
            {
                flag = !flag;
            }
            else
            {
                cin >> a >> b;
                int lefta;
                int righta;
                int leftb;
                int rightb;
                lefta = L[a];
                righta = R[a];
                leftb = L[b];
                rightb = R[b];
                if (op == 3 && R[b] == a)swap(a, b);
                if (op != 3 && flag == 1)
                {
                    op = 3 - op;
                }
                if (op == 1 && a == leftb)
                    continue;
                if (op == 2 && a == rightb)
                    continue;
                switch (op)
                {
                case 1:
                    link(lefta, righta);
                    link(leftb, a);
                    link(a, b);
                    break;
                case 2:
                    link(lefta, righta);
                    link(b, a);
                    link(a, rightb);
                    break;
                case 3:
                    if (righta == b)
                    {
                        link(lefta, b);
                        link(b, a);
                        link(a, rightb);
                    }
                    else
                    {
                        link(lefta, b);
                        link(b, righta);
                        link(leftb, a);
                        link(a, rightb);
                    }
                    break;
                }
            }
        }
        int temp = 0;
        long long ans = 0;
        for (int i = 1;i <= n;i++)
        {
            temp = R[temp];
            if (i % 2 == 1)
                ans += temp;
        }
        if (flag&&n % 2 == 0)
            ans =(long long) n*(n + 1) / 2 - ans;
        printf("Case %d: %lld\n", cas++, ans);
    }
    return 0;
}
*/


int q[1000005];
int vs_main()
{
    int n, m;
    int cas = 1;
    while (cin >> n >> m)
    {
        for (int i = 1;i <= n;i++)
        {
            q[i] = i;
        }
        int temp1;
        int temp2;
        int tmp;
        for (int i = 0;i < m;i++)
        {
            int name;
            cin >> name;
            if (name != 4)
            {
                cin >> temp1 >> temp2;
            }
            int pos1, pos2;
            switch (name)
            {
            case 1:
                for (int i = 1;i <= n;i++)
                {
                    if (temp1 == q[i])
                    {
                        pos1 = i;
                    }
                }
                for(int i=pos1;ipos2-1;i--)
                    q[i] = q[i-1];
                q[pos2-1] =temp1;
                break;
            case 2:
                for (int i = 1;i <= n;i++)
                {
                    if (temp1 == q[i])
                    {
                        pos1 = i;
                    }
                }
                for (int i = pos1;ipos2+1;i--)
                    q[i] = q[i - 1];
                q[pos2 +1] = temp1;
                break;
            case 3:
                for (int i = 1;i <= n;i++)
                {
                    if (temp1 == q[i])
                    {
                        pos1 = i;
                    }
                }
                for (int i = 1;i <= n;i++)
                {
                    if (temp2 == q[i])
                    {
                        pos2 = i;
                    }
                }
                tmp = q[pos1];
                q[pos1] = q[pos2];
                q[pos2] = tmp;
                break;
            case 4:
                for (int i = 1;i <=n / 2;i++)
                {
                    tmp = q[i];
                    q[i] = q[n - i + 1];
                    q[n - i + 1] = tmp;
                }
                break;
            }
        }
        long long sum = 0;
        for (int i = 1;i <= n;i = i + 2)
        {
            sum += q[i];
        }
        printf("Case %d: %lld\n", cas++, sum);
    }
    return 0;
}

/*
int vs_main()
{
    srand((unsigned)time(NULL));
    for (int j = 0;j <3;j++)
    {
        int n = rand()%100+1;
        int m = rand()%20+1;
        cout << n << " " << m << endl;
        for (int i = 0;i < m;i++)
        {
            int name = rand() % 4 + 1;
            cout << name;
            if (name != 4)
            {
                int temp1=rand() % n + 1;
                int temp2;
                cout <<" "<
相关文章
最新文章
热点推荐