UVA 10718 bit Mask (bit operation greedy)

Source: Internet
Author: User

This topic I did not think about to see the problem, the reason is just because I think I can not think out, really no confidence ah ... Well, I really didn't drill into the subject.

Perseverance... Slowly cultivate, can not do so, can not easily see the puzzle ah ...

Analysis:

First of all, the topic has already said that the enumeration will definitely time out, so, what you see on the net is an enumeration of 32 bits, and then with N, to determine whether the current bit is 1 or 0,

If it is 0, it will be replaced by 1, unless the maximum value of the interval is greater than 1. Because one can guarantee the final or maximum result.

If it is 1, it will be replaced by 0 (this will ensure that the result is the same as the minimum number of selected) unless the maximum value of 0 is smaller than the interval minimum,

This means that all bits behind the current bit are 1 and smaller than the minimum value.

Think about it later.

Post code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <set> #include <string> #include <algorithm>using namespace std; int main () {Long long int m,t,temp,i,n,l,u;while (CIN >> n >> L >> U) {m = 0;for (i=31; i>=0; i--) {t = (L Ong long) 1 << i;//only think of 1, can be used to determine whether one is 1 or 0temp = (m|t);//Place the position at 1if ((n&t) ==0) {if (temp <= U)// If the bit is 0 and 1 is smaller than the interval maximum, small m = temp;} else {if (M+t-1 < L)//If the bit is 1 and the maximum reached after 0 is set to 1 less than the interval minimum, or 0m = temp;}} cout << m << Endl;} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 10718 bit Mask (bit operation greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.