洛谷p1955 (NOI2015)

2 篇文章 0 订阅
1 篇文章 0 订阅

题目思路并不复杂…只要想到并查集就好…
不过还有一点…xi和xj数可能很大,那么我们是不可能搞那么大的数组的…所有应该利用map或者哈希之类的离散化到一个比较小的范围内…
对于所有要求相等的条件,把xi和xj合并,所有相等的都合并之后,开始检查不相等的条件,此时如果发现条件中有一组数之前已经被合并了,那么说明这个问询是有矛盾存在的。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
struct query {
    int x, y;
};
query q[1000001];
int par[2000005];
int find(int x)
{
    if (par[x] == x)return x;
    else return par[x] = find(par[x]);
}
void unite(int x, int y)
{
    x = find(x), y = find(y);
    if (x == y)return;
    par[y] = x;
}
int main()
{
    int t, n, i, j;
    cin >> t;
    while (t--) {
        cin >> n; int cnt1 =0, cnt2 = 0;
        for (i = 1; i <= 2*n; i++)
            par[i] = i;
        map<int, int>hash1;
        map<int, int>::iterator iter;
        for (i = 1; i <= n; i++) {
            int a, b, c;
            scanf("%d %d %d", &a, &b, &c);
            if ((iter = hash1.find(a)) == hash1.end()) {
                hash1.insert(pair<int, int>(a, ++cnt1)); a = cnt1;
            }
            else
                a = iter->second;
            if ((iter = hash1.find(b)) == hash1.end()) {
                hash1.insert(pair<int, int>(b, ++cnt1)); b = cnt1;
            }
            else
                b = iter->second;
            if (c == 1)
                //q1[++cnt1].x = a, q1[cnt1].y = b;
                unite(a, b);
            else
                q[++cnt2].x = a, q[cnt2].y = b;
        }
        bool check = true;
        for (i = 1; i <= cnt2; i++) {
            if (find(q[i].x) == find(q[i].y)) {
                check = false; break;
            }
        }
        if (check)cout << "YES" << endl;
        else cout << "NO" << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
P2375 [NOI2014] 动物园是一道经典的动态规划题目,以下是该题的详细题意和解题思路。 【题意描述】 有两个长度为 $n$ 的整数序列 $a$ 和 $b$,你需要从这两个序列中各选出一些数,使得这些数构成一个新的序列 $c$。其中,$c$ 序列中的元素必须在原序列中严格递增。每个元素都有一个价值,你的任务是选出的元素的总价值最大。 【解题思路】 这是一道经典的动态规划题目,可以采用记忆化搜索的方法解决,也可以采用递推的方法解决。 记忆化搜索的代码如下: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN], a[MAXN], b[MAXN], n; int dfs(int x, int y) { if (dp[x][y] != -1) return dp[x][y]; if (x == n || y == n) return 0; int res = max(dfs(x + 1, y), dfs(x + 1, y + 1)); if (a[x] > b[y]) { res = max(res, dfs(x, y + 1) + b[y]); } return dp[x][y] = res; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); memset(dp, -1, sizeof(dp)); printf("%d\n", dfs(0, 0)); return 0; } ``` 其中,dp[i][j]表示选到a数组中第i个元素和b数组中第j个元素时的最大价值,-1表示未计算过。dfs(x,y)表示选到a数组中第x个元素和b数组中第y个元素时的最大价值,如果dp[x][y]已经计算过,则直接返回dp[x][y]的值。如果x==n或者y==n,表示已经遍历完一个数组,直接返回0。然后就是状态转移方程了,如果a[x] > b[y],则可以尝试选b[y],递归调用dfs(x, y+1)计算以后的最大价值。否则,只能继续遍历数组a,递归调用dfs(x+1, y)计算最大价值。最后,返回dp[0][0]的值即可。 递推的代码如下: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN], a[MAXN], b[MAXN], n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]); if (a[i] > b[j]) { dp[i][j] = max(dp[i][j], dp[i][j + 1] + b[j]); } } } printf("%d\n", dp[0][0]); return 0; } ``` 其中,dp[i][j]表示选到a数组中第i个元素和b数组中第j个元素时的最大价值。从后往前遍历数组a和数组b,依次计算dp[i][j]的值。状态转移方程和记忆化搜索的方法是一样的。 【参考链接】 P2375 [NOI2014] 动物园:https://www.luogu.com.cn/problem/P2375

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值