CF1634 B. Fortune Telling(思维+位运算)

 

大致题意 : 给定一串长度为n的数组, 在给定x, y, 对于数组的每一个数, 可以用x进行两种操作,

操作一 : x = x + a[i],

操作二 : x = x xor a[i];

问对 x 和 x+3 哪一个数可以通过对数组操作得到y

分析 : 一开始想模拟, 写发现时间复杂度肯定会爆, 我们可以对数据进行分析, 给定y 求x, x+3, 数据肯定是合理的, 对于x, x + 3我们一定可以得到一个y, 那么x 与 x + 3最大的不同是什么, 没错就是奇偶性, 最后操作完, x 或 x+3 的奇偶性一定是和y相等, 再对操作进行分析可以发现, 无论是+ 还是 xor, 他对奇偶性的影响都是一样的(可以列一个真值表), 所以我们可以遍历数组, 对x进行某种相同的操作, 其奇偶性与答案一样, 这里选择xor, 是因为数据范围很大, 若用+会爆数据

AC代码如下:

#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

int n;
ll x,y;

int main()
{
    int T;
    scanf("%d", &T);
    while(T -- )
    {
        scanf("%d%lld%lld", &n, &x, &y);
        for(int i = 0; i<n; i++)
        {
            ll a;
            scanf("%lld", &a);
            x ^= a;
        }
//        cout<<x<<" "<<(x>>1)&1<<" "<<y<<" "<<(y>>1) & 1<<endl;
        if(x%2){
            if(y%2) cout<<"Alice"<<endl;
            else cout<<"Bob"<<endl;
        }else{
            if(y%2) cout<<"Bob"<<endl;
            else cout<<"Alice"<<endl;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.*;public class Billionaires { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Map<String, List<Billionaire>> billionairesByCity = new HashMap<>(); for (int i = 0; i < n; i++) { String name = sc.next(); String city = sc.next(); long fortune = sc.nextLong(); Billionaire billionaire = new Billionaire(name, city, fortune); if (!billionairesByCity.containsKey(city)) { billionairesByCity.put(city, new ArrayList<>()); } billionairesByCity.get(city).add(billionaire); } int m = sc.nextInt(); int k = sc.nextInt(); for (int i = 0; i < k; i++) { int day = sc.nextInt(); String name = sc.next(); String city = sc.next(); List<Billionaire> billionaires = billionairesByCity.get(city); for (Billionaire billionaire : billionaires) { if (billionaire.name.equals(name)) { billionaire.addTravel(day); break; } } } List<String> cities = new ArrayList<>(billionairesByCity.keySet()); Collections.sort(cities); for (String city : cities) { List<Billionaire> billionaires = billionairesByCity.get(city); Map<Integer, Long> wealthByDay = new HashMap<>(); for (Billionaire billionaire : billionaires) { for (int day : billionaire.travels) { wealthByDay.put(day, wealthByDay.getOrDefault(day, 0L) + billionaire.fortune); } } int maxDays = 0; for (int day : wealthByDay.keySet()) { long wealth = wealthByDay.get(day); for (int i = day + 1; i <= m; i++) { long futureWealth = wealthByDay.getOrDefault(i, 0L); if (futureWealth > wealth) { break; } if (i - day > maxDays) { maxDays = i - day; } } } if (maxDays > 0) { System.out.println(city + " " + maxDays); } } } static class Billionaire { String name; String city; long fortune; List<Integer> travels = new ArrayList<>(); public Billionaire(String name, String city, long fortune) { this.name = name; this.city = city; this.fortune = fortune; } public void addTravel(int day) { travels.add(day); } }}的计算复杂度是多少
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值