[Arc074E] RGB Sequence

[Arc074E] RGB Sequence

Description

今天也在愉快地玩Minecraft!现在MM有一块1?N的空地,每个格子按照顺序标记为1到N。MM想要在这块空地上铺上红石块、绿宝石块和钻石块作为装饰。每个格子只能选择一种方块。MM有自己的审美标准。他定下了M条规定,每条规定形如(li,ri,xi),表示闭区间[li,ri]中,需要有恰好xi种不同的方块。MM觉得这个任务实在是太简单了,于是把它交给了你,但是你发现有太多种方式可以满足MM的审美需求了!于是你希望先知道,一共有多少铺方块的方法,可以满足MM的审美需求?答案对109+7取模

Input

第一行两个整数,N,M
接下来M行,每行三个整数li,ri,xi

Output

一个整数,对109+7取模后的答案

Sample Input

Case 1:3 1
1 3 3
Case 2:4 2
1 3 1
2 4 2
Case 3:1 3
1 1 1
1 1 2
1 1 3
Case 4:8 10
2 6 2
5 5 1
3 5 2
4 7 3
4 4 1
2 3 1
7 7 1
1 5 2
1 7 3
3 4 2

Sample Output

Case 1:6
Case 2:6
Case 3:0
Case 4:108

HINT

1≤N,M≤300
1≤li≤ri≤N
1≤xi≤3

试题分析

(f{i,j,k})表示填到第i位,另外两种颜色最后一次涂在({j,k})的方案数。
在右端点判断不合法情况,转移即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;
#define LL long long
 
inline int read(){
    int x=0,f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
const int INF = 2147483600;
const int MAXN = 100010;
const int Mod = 1e9+7;
 
int N,M;
struct data{
    int l,x; data(int ll=0,int xx=0){
        l=ll; x=xx;
    }
}; vector<data> vec[MAXN+1];
inline bool check(int i,int j,int k){
    for(int l=0;l<vec[i].size();l++){
        int cnt=1; if(vec[i][l].l<=j) ++cnt;
        if(vec[i][l].l<=k) ++cnt;
        if(cnt!=vec[i][l].x) return false;
    } return true;
}int f[301][301][301];
 
int main(){
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    N=read(),M=read();
    for(int i=1;i<=M;i++){
        int l=read(),r=read(),x=read();
        vec[r].push_back(data(l,x));
    } f[0][0][0]=1;
    for(int i=0;i<N;i++){
        for(int j=0;j<(i==0?1:i);j++){
            for(int k=0;k<(j==0?1:j);k++){
                if(!check(i,j,k)){f[i][j][k]=0; continue;}
                (f[i+1][i][j]+=f[i][j][k])%=Mod;
                (f[i+1][i][k]+=f[i][j][k])%=Mod;
                (f[i+1][j][k]+=f[i][j][k])%=Mod;
            }
        }
    } int ans=0;
    for(int i=0;i<=N;i++){
        for(int j=i+1;j<=N;j++)
            if(check(N,j,i)) (ans+=f[N][j][i])%=Mod;
    }if(check(N,0,0)) (ans+=f[N][0][0])%=Mod;
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/wxjor/p/9476922.html