zoukankan      html  css  js  c++  java
  • 【【henuacm2016级暑期训练】动态规划专题 I】Gargari and Permutations

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    注意这k个序列每个都是排列。 如果在每个序列中都满足y出现在x之后的话。 那么我们从x连一条有向边至y (有一个序列不满足就不连 (这就表明最后的序列中x可以紧接着y

    最后显然会形成一个有向无环图。
    在这个图上求最长链就好了。

    可以在做拓扑排序的时候边做这个dp.

    【代码】

    #include <bits/stdc++.h>
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define all(x) x.begin(),x.end()
    #define pb push_back
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    using namespace std;
    
    const double pi = acos(-1);
    const int dx[4] = {0,0,1,-1};
    const int dy[4] = {1,-1,0,0};
    const int N = 1000;
    
    int n,k,a[N+10],ru[N+10],f[N+10];
    bool bo[N+10][N+10];
    queue<int> dl;
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
        cin >> n >> k;
        memset(bo,true,sizeof bo);
        for (int i = 1;i <= n;i++) ru[i] = n-1;
        for (int i = 1;i <= k;i++){
            for (int i = 1;i <= n;i++) cin >> a[i];
            for (int i = 1;i <= n;i++){
                for (int j = 1;j <= i-1;j++){
                    if (bo[a[i]][a[j]]==true) {
                        ru[a[j]]--;
                    }
                    bo[a[i]][a[j]] = false;
                }
            }
        }
    
        for (int i = 1;i <= n;i++)
            if (ru[i]==0){
                dl.push(i);
                f[i] = 1;
                ru[i]=-1;
            }
        while (!dl.empty()){
            int x = dl.front();dl.pop();
            for (int i = 1;i <= n;i++)
                if (i!=x && bo[x][i]){
                    ru[i]--;
                    f[i] = max(f[i],f[x]+1);
                    if (ru[i]==0){
                        ru[i] = -1;
                        dl.push(i);
                    }
                }
        }
        int ans = 0;
        for (int i = 1;i <= n;i++) ans = max(ans,f[i]);
        cout<<ans<<endl;
    	return 0;
    }
    
    
  • 相关阅读:
    css实现右侧三角
    css 应用 给头像添加标签
    css中伪选择器使用
    服务器上的项目不能对外发送请求
    视频只能播放一两帧,PDF文件60k以上加载失败的问题,静态访问也一样
    spring集成mybatis 打印sql语句
    新浪股票代码
    linux 设置mysql密码
    linux启动mysql服务报错
    配置eclipseJVM虚拟机内存大小
  • 原文地址:https://www.cnblogs.com/AWCXV/p/9317803.html
Copyright © 2011-2022 走看看