poj 1068 Parencodings

没什么算法,直接模拟。

/*
 * Author: stormdpzh
 * Time: 2012/5/11 20:07:00
 * POJ: 1068 Parencodings
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define wh(n) while(scanf("%d", &n) != EOF)
#define whz(n) while(scanf("%d", &n) != EOF && n != 0)
#define lint long long

using namespace std;

const int MaxN = 25;

struct Node {
    int no;
    int data;
    
    Node(int _no, int _data) : no(_no), data(_data) {}
    
    bool operator < (const Node &t) const {
        return no < t.no;
    }
};
set<Node> st;
int n;
int p[MaxN], w[MaxN];
int seq[(MaxN << 1)];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        rep(i, n) scanf("%d", &p[i]);
        st.clear();
        int last = 0;
        int k = 0;
        rep(i, n) {
            rep(j, p[i] - last) st.insert(Node(k++, 0));
            st.insert(Node(k++, 1));
            last = p[i];
        }
        set<Node>::iterator it;
        k = 0;
        for(it = st.begin(); it != st.end(); it++) {
            if((*it).data == 1) seq[k] = 1;
            else seq[k] = 0;
            k++;
        }
        int id = 0;
        rep(i, k) {
            if(seq[i] == 1) {
                int dis = 1;
                repd(j, i, 0) {
                    if(seq[j] == 0) {
                        seq[j] = -1;
                        break;
                    }
                    else if(seq[j] == -1) dis++;
                }
                w[id++] = dis;
            }
        }
        rep(i, id) {
            if(i == 0) printf("%d", w[i]);
            else printf(" %d", w[i]);
        }
        printf("\n");
    }
    
    return 0;
}

你可能感兴趣的:(算法,struct,iterator)