codeforces 567 F. Mausoleum (dp)

F. Mausoleum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

King of Berland Berl IV has recently died. Hail Berl V! As a sign of the highest achievements of the deceased king the new king decided to build a mausoleum with Berl IV's body on the main square of the capital.

The mausoleum will be constructed from 2n blocks, each of them has the shape of a cuboid. Each block has the bottom base of a 1 × 1 meter square. Among the blocks, exactly two of them have the height of one meter, exactly two have the height of two meters, ..., exactly two have the height of n meters.

The blocks are arranged in a row without spacing one after the other. Of course, not every arrangement of blocks has the form of a mausoleum. In order to make the given arrangement in the form of the mausoleum, it is necessary that when you pass along the mausoleum, from one end to the other, the heights of the blocks first were non-decreasing(i.e., increasing or remained the same), and then — non-increasing (decrease or remained unchanged). It is possible that any of these two areas will be omitted. For example, the following sequences of block height meet this requirement:

  • [1, 2, 2, 3, 4, 4, 3, 1];
  • [1, 1];
  • [2, 2, 1, 1];
  • [1, 2, 3, 3, 2, 1].

Suddenly, k more requirements appeared. Each of the requirements has the form: "h[xisigni h[yi]", where h[t] is the height of the t-th block, and a signi is one of the five possible signs: '=' (equals), '<' (less than), '>' (more than), '<=' (less than or equals), '>=' (more than or equals). Thus, each of the k additional requirements is given by a pair of indexes xiyi (1 ≤ xi, yi ≤ 2n) and sign signi.

Find the number of possible ways to rearrange the blocks so that both the requirement about the shape of the mausoleum (see paragraph 3) and the k additional requirements were met.

Input

The first line of the input contains integers n and k (1 ≤ n ≤ 35, 0 ≤ k ≤ 100) — the number of pairs of blocks and the number of additional requirements.

Next k lines contain listed additional requirements, one per line in the format "xi signi yi" (1 ≤ xi, yi ≤ 2n), and the sign is on of the list of the five possible signs.

Output

Print the sought number of ways.

Sample test(s)
input
3 0
output
9
input
3 1
2 > 3
output
1
input
4 1
3 = 6
output
3

很容易看出来是dp

我们左右一起,一对一对放.

对于每一对,有三种方法,分别是两左,一左一右,两右.

初始区间长度为2n,每次放完后缩小区间长度 ,最后一定是放2个n,这个时候区间长度缩小为2,表明一种满足题意的情况.

状态转移的时候需要分别判断三个状态是否满足题目中给出的k组限制条件.

细节见注释.

/*************************************************************************
    > File Name: code/cf/#314/F.cpp
    > Author: 111qqz
    > Email: rkz2013@126.com 
    > Created Time: 2015年08月16日 星期日 14时37分15秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)  
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N = 1E2+7;
int n, k, a[N], b[N];
LL dp[N][N];
string sign[N];
int L, R, F, S;
enum 
{
    OLD, CUR, NEW
};
int get_type(int i) 
{
    if (i < L || i > R) return OLD;
    if (i == F || i == S) return CUR;
    return NEW;
}
bool compare(int a, int b, string s) 
{
    if (s == "=") return a == b;
    if (s == ">") return a > b;
    if (s == "<") return a < b;
    if (s == ">=") return a >= b;
    if (s == "<=") return a <= b;
}
bool check(int l, int r, int f, int s)
{
    L = l, R = r;
    F = f, S = s;
    for (int i = 0; i < k; i++) 
    {
        int lf = get_type(a[i]); // 判断对于当前要添加的位置,是否有题目中给出限制的位置,如果是,判断是否满足限制.
                //如果有一个限制条件不满足就不成立,所有限制条件都满足才成立.
        int rg = get_type(b[i]);
        if (lf != CUR && rg != CUR) continue;
        if (!compare(lf, rg, sign[i])) return false;
    }
    return true;
}
LL cal(int l, int r) 
{
    LL &res = dp[l][r];//dp[l][r] 表示在区间[l,r]放置的方案的个数
    if (res != -1) return res;
    res = 0;
    if (l + 1 == r) //最后相邻,表示最后两个n放在一起了,答案+1
    {
        if (check(l, r, l, r)) res++;
    } else 
    {
        if (check(l, r, l, l + 1)) res += cal(l + 2, r); //一对的两个都放在左边
        if (check(l, r, l, r)) res += cal(l + 1, r - 1); //左1右1
        if (check(l, r, r - 1, r)) res += cal(l, r - 2);// 右2
    }
    return res;
}
int main () 
{
    scanf("%d %d", &n, &k);
    n = n * 2;
    for (int i = 0; i < k; i++) 
    {
        cin>>a[i]>>sign[i]>>b[i];
        a[i]--;
        b[i]--;
    }
    memset(dp, -1, sizeof(dp));
    printf("%I64d
",cal(0,n-1));
    return 0;
}
原文地址:https://www.cnblogs.com/111qqz/p/4734369.html