[bzoj3668][Noi2014]起牀困難綜合症【貪心】【模擬】

【題目鏈接】
  https://www.lydsy.com/JudgeOnline/problem.php?id=3668
  http://uoj.ac/problem/2
【題解】
  依次枚舉每一位,若填1比填0大,填1。否則爲了之後能填1,填0。
  時間複雜度O(30N)
【代碼】

/* - - - - - - - - - - - - - - -
    User :      VanishD
    problem :   [noi2014][bzoj3668] 
    Points :    
- - - - - - - - - - - - - - - */
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    N       100100
# define    T       30
using namespace std;
int read(){
    int tmp = 0, fh = 1; char ch = getchar();
    while (ch < '0' || ch > '9'){ if (ch == '-') fh = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9'){ tmp = tmp * 10 + ch - '0'; ch = getchar(); }
    return tmp * fh;
}
int n, m, num, ans, op[N], sum[N];
char x[11];
int did(int x, int id){
    for (int i = 1; i <= n; i++){
        int tmp = ((sum[i] & (1 << id)) >> id);
        if (op[i] == 0) x = x ^ tmp;
        if (op[i] == 1) x = x | tmp;
        if (op[i] == 2) x = x & tmp;
    }
    return (x << id);
}
int main(){
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
    n = read(), m = read();
    for (int i = 1; i <= n; i++){
        scanf("\n%s", x + 1);
        if (x[1] == 'X') op[i] = 0;
        if (x[1] == 'O') op[i] = 1;
        if (x[1] == 'A') op[i] = 2;
        sum[i] = read(); 
    }
    for (int i = T; i >= 0; i--){
        int tmp0 = did(0, i), tmp1 = did(1, i);
        if (num + (1 << i) <= m){
            if (tmp0 >= tmp1)
                ans = ans + tmp0;
                else ans = ans + tmp1, num = num + (1 << i);
        }
        else ans = ans + tmp0;
    }
    printf("%d\n", ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章