zoukankan      html  css  js  c++  java
  • Similar Arrays CodeForces

    Similar Arrays

    题意:

    题目要构造两个序列

    A序列每个数字都不相同,B序列只有两个相同的数字

    给M组关系 i ,j表示 ai > aj 或者 ai < aj

    同时B序列有这个和A序列一样的关系 如果 ai > aj 就有 bi > bj

    思路:

    首先考虑不能构造的情况 ,如果A序列中任意一对数字都存在关系,那么B序列中不可能由两个相同的数字

    即 m >= (n - 1) * n / 2

    如果 m < (n - 1) * n / 2 ,找两个没有关系的点,A序列赋值为n 和 n - 1 ,  B序列赋值为 n 和 n 

    剩下的A、B序列都一样就好了

      1 #include<cstdio>
      2 #include<string.h>
      3 #include<algorithm>
      4 #include<cmath>
      5 #include<iostream>
      6 #include<vector>
      7 #include<queue>
      8 #include<set>
      9 #include<map>
     10 #include<cctype>
     11 #define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
     12 #define mem(a,x) memset(a,x,sizeof(a))
     13 #define lson rt<<1,l,mid
     14 #define rson rt<<1|1,mid + 1,r
     15 #define P pair<int,int>
     16 #define ull unsigned long long
     17 using namespace std;
     18 typedef long long ll;
     19 const int maxn = 2e5 + 10;
     20 const ll mod = 998244353;
     21 const int inf = 0x3f3f3f3f;
     22 const long long INF = 0x3f3f3f3f3f3f3f3f;
     23 const double eps = 1e-7;
     24 inline ll read()
     25 {
     26     ll X = 0, w = 0; char ch = 0;
     27     while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
     28     while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
     29     return w ? -X : X;
     30 }
     31 ll n, m , a , b;
     32 vector<int>vec[maxn], v;
     33 ll cnt[maxn], arr1[maxn] , arr2[maxn] , cnt1[maxn];
     34 
     35 
     36 
     37 int main()
     38 {
     39     n = read(), m = read();
     40     for (int i = 1; i <= m; ++i)
     41     {
     42         int u = read(), v = read();
     43         vec[u].push_back(v);
     44         vec[v].push_back(u);
     45     }
     46     if (2 * m >= n * (n - 1))
     47     {
     48         cout << "NO" << endl;
     49         return 0;
     50     }
     51     int anspos;
     52     for (int i = 1; i <= n; ++i)
     53     {
     54         if (vec[i].size() == n - 1) continue;
     55         else
     56         {
     57             anspos = i;
     58             for (int j = 0; j < vec[i].size(); ++j) cnt[vec[i][j]] = 1;
     59             break;
     60         }
     61     }
     62     for (int i = 1; i <= n; ++i)
     63     {
     64         if (cnt[i] == 0 && i != anspos)
     65         {
     66             arr2[anspos] = arr2[i] = n;
     67             arr1[anspos] = n, arr1[i] = n - 1;
     68             break;
     69         }
     70     }
     71     int res = 0;
     72     for (int i = 1; i <= n; ++i)
     73     {
     74         if (arr1[i] == 0) arr1[i] = ++res;
     75     }
     76     res = 0;
     77     for (int i = 1; i <= n; ++i)
     78     {
     79         if (arr2[i] == 0) arr2[i] = ++res;
     80     }
     81     cout << "YES" << endl;
     82     for (int i = 1; i <= n; ++i)
     83     {
     84         cout << arr1[i];
     85         if (i == n) cout << endl;
     86         else cout << " ";
     87     }
     88     for (int i = 1; i <= n; ++i)
     89     {
     90         cout << arr2[i];
     91         if (i == n) cout << endl;
     92         cout << " ";
     93     }
     94 
     95 }
     96 
     97 
     98 /*
     99 4 5
    100 11 12 13 14 15
    101 0 4 10 15 20
    102 2 10 30 32 40
    103 20 21 22 23 24
    104 */
    AC代码
  • 相关阅读:
    Truck History(poj 1789)
    Highways poj 2485
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    115. Distinct Subsequences
    114. Flatten Binary Tree to Linked List
    113. Path Sum II
    109. Convert Sorted List to Binary Search Tree
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/DreamACMer/p/12707644.html
Copyright © 2011-2022 走看看