首页 > 其他分享 > Codeforces Round #816 (Div. 2)

Codeforces Round #816 (Div. 2)

时间:2023-01-28 11:45:09浏览次数:43  
标签:那位 标记 int Codeforces 对应 ++ Div operatorname 816

D. 2+ doors

要让字典序最小就要让每个数字在满足条件的同时都尽可能的小并且排在前面的数字变小的优先级要比排在后面的数字的优先级更大。

\[\begin{aligned} 1\operatorname|1 &= 1\\ 0\operatorname|1 &= 1\\ 1\operatorname|0 &= 1\\ 0\operatorname|0 &= 0\\ \end{aligned} \]

先排序,让下标小的限定条件排在前面,等下才方便贪心。

如果 \(i\operatorname|j = x\) 那么当 \(x\) 二进制下某位为 \(0\) 时,\(i\) 和 \(j\) 对应的那位也必须是 \(0\) 我们用一个二维数组 \(a\) 来标记这些必须为 \(0\) 的位,标记为 \(2\)。

全部标记完以后再遍历一次,如果 \(x\) 二进制下某位为 \(1\),若 \(i = j\),那 \(i\) 对应的那位肯定是 \(1\);如果 \(i\) 和 \(j\) 不相等,那么 \(i\) 对应的那位为 \(1\) 或 \(j\) 对应的那位为 \(1\);如果 \(i\) 对应的那位已经被标记为 \(2\) 了,那么肯定是 \(j\) 对应的那位为 \(1\),否则反之;如果是 \(1\) 那么标记为 \(1\)。

最后只剩一种情况,如果 \(x\) 二进制下某位为 \(1\) 且 \(i\) 和 \(j\) 对应的那位既没有被标记为 \(2\) 也没有被标记为 \(1\), 这种情况肯定是 \(j\) 对应的那位标记为 \(1\)。

#include "bits/stdc++.h"

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<array<int, 3>> q(m);

  for (int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--;
    y--;
    if (x > y) {
      swap(x, y);
    }
    q[i] = {x, y, w};
  }

  sort(q.begin(), q.end());

  vector<array<int, 30>> a(n);

  for (int i = 0; i < m; i++) {
    auto [x, y, w] = q[i];
    for (int j = 0; j < 30; j++) {
      if ((w >> j & 1) == 0) {
        a[x][j] = 2;
        a[y][j] = 2;
      }
    }
  }

  for (int i = 0; i < m; i++) {
    auto [x, y, w] = q[i];
    for (int j = 0; j < 30; j++) {
      if (w >> j & 1) {
        if (x == y) {
          a[x][j] = 1;
          a[y][j] = 1;
        } else if (a[x][j] == 2) {
          a[y][j] = 1;
        } else if (a[y][j] == 2) {
          a[x][j] = 1;
        }
      }
    }
  }

  for (int i = 0; i < m; i++) {
    auto [x, y, w] = q[i];
    for (int j = 0; j < 30; j++) {
      if (w >> j & 1) {
        if (a[x][j] == 0 && a[y][j] == 0) {
          a[y][j] = 1;
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    int res = 0;
    for (int j = 29; j >= 0; j--) {
      res *= 2;
      if (a[i][j] == 2) {
        a[i][j] = 0;
      }
      res += a[i][j];
    }
    cout << res << " \n"[i == n - 1];
  }

  return 0;
}

标签:那位,标记,int,Codeforces,对应,++,Div,operatorname,816
From: https://www.cnblogs.com/kiddingma/p/17069996.html

相关文章

  • Codeforces Round #847 (Div. 3)
    E.VladandaPairofNumbers题目抽象为给\(n\)\((1\len\le2^{29})\),求\(x\)满足\((n-x)\oplus(n+x)=n\),输出\(n-x\)和\(n+x\)。显然\(n\)为奇数肯定不行......
  • CF 1790E. Vlad and a Pair of Numbers_Codeforces Round #847 (Div. 3)
    给出整数x,求一对整数(a,b),满足:\(a\bigoplusb=x\),\(\frac{a+b}{2}=x\)(\(\frac{a+b}{2}\)不四舍五入,也就是\(2\mida+b\))如果不存在这样的(a,b)输出-1分析:如果x的最......
  • # CF#847 (Div. 3)ABCDE题解
    CodeforcesRound#847(DFiv.3)APolycarpandtheDayofPiProblem-A-Codeforces题目描述OnMarch14,thedayofthenumber$\pi$iscelebratedallov......
  • Educational Codeforces Round 2 个人总结A-E
    EducationalCodeforcesRound2A.ExtractNumbers简单的模拟boolcheck(stringop){ if(op.size()==1&&op[0]=='0') returntrue; if(op.size()==0||(op[0]<'1......
  • #0031. Educational Codeforces Round 1
    AB简单题C是计算几何,但核心解法很像sgnoi某年的t1,即与其考虑所有pairs,不如只考虑所有相邻的,这样复杂度就从\(O(N^2)\)降到了O(N)(如果不考虑排序的复杂度的话)。不过这......
  • Educational Codeforces Round 142
    目录写在前面ABCDE写在最后写在前面比赛地址:https://codeforces.com/contest/1792。我是超级大鸽子咕咕咕A当且仅当有两个怪物初始血量为1时使用操作1,否则用操作2......
  • Codeforces Round #846 (Div. 2)
    题目链接D题意给定一个数字n,我们需要通过一系列的操作去猜测这个数字。初始的时候会给我们一个cnt,表示二进制下数字n有多少个1.每次我们可以给出一个t,然后另n减去t,系统......
  • Codeforces Round #601 (Div. 2) A-E
    比赛链接A题意给两个数字\(a,b\),每次操作可以使\(a\)加上\(+1,+2,+5,-1,-2,-5\)中的一个数,求最少多少次操作可以将\(a\)变成\(b\)。题解知识点:贪心。可以......
  • CodeForces 1415E New Game Plus!
    洛谷传送门CF传送门相当于将\(n\)个数分成\(k+1\)组,将每组的最大收益相加。容易发现组内的数不增最优。考虑开个堆,维护当前\(k+1\)组的和即可。code/*p_b_......
  • CodeForces 1415D XOR-gun
    洛谷传送门CF传送门纯纯的诈骗。下文令\(f(x)\)为\(x\)最高位使得这一位为\(1\)。考虑若存在\(i\in[1,n-2]\)使得\(f(a_i)=f(a_{i+1})=f(a_{i+2})\),那么......