hdu 5071 Chat

无限WA。。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <set>
#include <list>
#include <map>
#include <limits>
using namespace std;

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define REPOK(i, s, t, o) for(int (i)=(s);(i)<=(t) && (o);++(i))
#define MAXN 10000
#define MAXM 50
#define MOD 10000000000007
#define PI 3.1415926535897932384626433832795
#define INF 0x7FFFFFFF

typedef long long LL;
typedef vector<int> veci;
typedef pair<int, int> pairi;
/***************BEGINHERE*****************/
int _prev[MAXN+5];
int _next[MAXN+5];
LL word[MAXN+5];
int property[MAXN+5];
int head, tail;
bool _empty;

bool always_top = false;
int top;

struct obj {
    bool operator () (const pairi & lhs, const pairi & rhs) const {
        return lhs.second > rhs.second;
    }
};

set<pair<int, int>, obj > myset;
map<int, int> _map;

int tot;
int _size;

enum{
    add_u = 1,
    close_u = 2,
    chat_w = 3,
    rotate_x = 4,
    prior = 5,
    choose_u = 6,
    top_u = 7,
    untop_u = 8
};

void solve(int type, int param) {
    switch (type) {
    case add_u:
        if (_map.count(param)) {
            printf("same priority.\n");
        } else {
            printf("success.\n");
            _map[param] = ++tot;
            property[tot] = param;
            word[tot] = 0;
            myset.insert(make_pair(tot, param));
            ++_size;
            if (_empty) {
                head = tot;
                tail = tot;
                _prev[head] = -1;
                _next[head] = -1;
                _prev[tail] = -1;
                _next[tail] = -1;
                _empty = false;
            } else {
                _next[tail] = tot;
                _prev[tot] = tail;
                _next[tot] = -1;
                tail = tot;
            }
        }
        break;
    case close_u:
        if (_map.count(param)) {
            int u = _map[param], printed = 0;
            printf("close %d with %lld.\n",param, word[u]);
            if (always_top && top == u) {
                if (word[top] > 0) {
                    printf("Bye %d: %lld\n", property[top], word[top]);
                    printed = 1;
                }
                always_top = false;
            }
            if (head == u) {
                if (!printed && word[head] > 0)
                    printf("Bye %d: %lld\n", property[head], word[head]);
                head = _next[head];
            }
            if (tail == u) {
                tail = _prev[tail];
                if (tail > 0)
                    _next[tail] = -1;
            }
            --_size;
            if (!_size)
                _empty = true;
            _map.erase(param);
            myset.erase(myset.find(make_pair(u, param)));
        } else {
            printf("invalid priority.\n");
        }
        break;
    case chat_w:
        if (_empty) {
            printf("empty.\n");
        } else {
            printf("success.\n");
            if (always_top) {
                word[top] += param;
                //printf("talk to %d with %d words.\n", top, param);
            }
            else {
                //printf("talk to %d with %d words.\n", head, param);
                word[head] += param;
            }
        }
        break;
    case rotate_x:
        if (param < 1 || param > _size) {
            printf("out of range.\n");
        } else {
            printf("success.\n");
            if (param == 1)
                break;
            int u = head;
            REP(i, 1, param-1)
                u = _next[u];
            _next[_prev[u]] = _next[u];
            if (u != tail)
                _prev[_next[u]] = _prev[u];
            else {
                tail = _prev[u];
                if (tail > 0)
                    _next[tail] = -1;
            }

            _prev[u] = -1;
            _next[u] = head;
            _prev[head] = u;
            head = u;
        }
        break;
    case prior:
        if (_empty) {
            printf("empty.\n");
        } else {
            printf("success.\n");
            int u = (*myset.begin()).first;
            if (head == u)
                break;
            _next[_prev[u]] = _next[u];
            if (u != tail)
                _prev[_next[u]] = _prev[u];
            else {
                tail = _prev[u];
                if (tail > 0)
                    _next[tail] = -1;
            }

            _prev[u] = -1;
            _next[u] = head;
            _prev[head] = u;
            head = u;
        }
        break;
    case choose_u:
        if (_map.count(param)) {
            printf("success.\n");
            int u = _map[param];
            if (head == u)
                break;
            _next[_prev[u]] = _next[u];
            if (u != tail)
                _prev[_next[u]] = _prev[u];
            else {
                tail = _prev[u];
                if (tail > 0)
                    _next[tail] = -1;
            }

            _prev[u] = -1;
            _next[u] = head;
            _prev[head] = u;
            head = u;
        } else {
            printf("invalid priority.\n");
        }
        break;
    case top_u:
        if (_map.count(param)) {
            printf("success.\n");
            int u = _map[param];
            always_top = true;
            top = u;
        } else {
            printf("invalid priority.\n");
        }
        break;
    case untop_u:
        if (always_top) {
            printf("success.\n");
            always_top = false;
        } else {
            printf("no such person\n");
        }
        break;
    }
    //printf("tail = %d\n",property[tail]);
}

int main() {
    freopen("input.in", "r", stdin);
    int t, n;
    cin >> t;
    char buf[100];
    int type, param;
    while(t--) {
        scanf("%d",&n);
        _map.clear();
        myset.clear();
        tot = 0;
        _empty = true;
        _size = 0;
        head = tail = -1;
        always_top = false;
        int option_cnt = 0;
        while(n--) {
            printf("Operation #%d: ",++option_cnt);
            scanf("%s",buf);
            param = 0;
            switch(buf[0]) {
            case 'A':
                type = add_u;
                scanf("%d",¶m);
                break;
            case 'C':
                scanf("%d",¶m);
                if (buf[1] == 'l') {
                    type = close_u;
                } else if (buf[1] == 'h' && buf[2] == 'a') {
                    type = chat_w;
                } else {
                    type = choose_u;
                }
                break;
            case 'R':
                scanf("%d",¶m);
                type = rotate_x;
                break;
            case 'P':
                type = prior;
                break;
            case 'T':
                scanf("%d",¶m);
                type = top_u;
                break;
            case 'U':
                type = untop_u;
                break;
            }
            solve(type, param);
        }
        if (!_empty) {
            int forbid = -1;
            if (always_top) {
                printf("Bye %d: %lld\n", property[top], word[top]);
                forbid = top;
            }
            for (int h = head;h != -1;h = _next[h])
                if (h != forbid) {
                    printf("Bye %d: %lld\n", property[h], word[h]);
                }

        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值