Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解題前提
a. u[i]為1,v[i]為1或0。
b. u[i]為0,v[i]為0。
a. 情況1,不連續進位,例如 0110+0100 = 1010。
b. 情況2,連續進位,例如 0110+0010 = 1000。
解題思路
判定條件
C++ Code
#include <bits/stdc++.h> using namespace std; int main() {ios::sync_with_stdio(false);int n;cin >> n;int x, y;while (n--) {// s1,s2分別存x和y中1的位置。 stack<int> s1, s2;cin >> x >> y;if (x > y) {cout << "NO" << endl;continue;}for (int i = 0; x != 0; ++i) {int t = x % 2;if (t == 1) s1.push(i);x /= 2;}for (int i = 0; y != 0; ++i) {int t = y % 2;if (t == 1) s2.push(i);y /= 2;}// 如果x中1比y中少,"NO"。 if (s1.size() < s2.size()) {cout << "NO" << endl;continue;}// 標志位,所有"NO"的條件都會導致flag變成false bool flag = true;while (s1.size() && s2.size()) {// 如果 u 中 1 在 v中1左邊,"NO"。因為u不能右移操作。 if (s1.top() > s2.top()) {flag = false;break;}s1.pop();s2.pop();if(s2.size() == 0) break;// 如果u中有多余的1,將其彈出。(多余=可連續進位) while (s1.size() && s1.top() > s2.top()) {s1.pop();} // 如果x中1比y中少,"NO"。if(s1.size() < s2.size()){flag = false;break;}}cout << (flag ? "YES" : "NO") << endl;}return 0; }總結
以上是生活随笔為你收集整理的Codeforces 1491 D. Zookeeper and The Infinite Zoo (二进制处理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 1479A. Se
- 下一篇: 如何出(改编)一道ACM算法题?