Codeforces-1230-D. Marcin and Training Camp(位运算+思维)

D. Marcin and Training Camp

Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.

Let’s focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers ai and bi; bi is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2j) is set in the binary representation of ai. Otherwise, this bit is not set.

Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn’t know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.

Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?

Input

The first line contains one integer n (1≤n≤7000) — the number of students interested in the camp.

The second line contains n integers. The i-th of them is ai (0≤ai<260).

The third line contains n integers. The i-th of them is bi (1≤bi≤109).

Output

Output one integer which denotes the maximum sum of bi over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0.

Examples

input

4
3 2 3 6
2 8 5 10

output

15

input

3
1 2 3
1 2 3

output

0

input

1
0
1

output

0

Note

In the first sample test, it’s optimal to send the first, the second and the third student to the camp. It’s also possible to send only the first and the third student, but they’d have a lower sum of bi.

In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.

解题思路:

分析题意以后可以发现,首先在数组中找到有重复的元素,记录所有的有重复的元素,然后遍历整个数组,对于每个元素,再遍历所有的有重复的元素,如果和重复元素T或起来等于T的,就是符合题意的,就把他的权值加起来就行了,找重复元素其实开个标记计数就好了,我还去排了个序,才把重复元素给找出来。然后还有一个操作就是把每个数的每一位都给找出来。于是乎又去写了一串代码取每一位。最后在判断的时候又写了一个函数去比对两个数的每一位。

AC代码:

#include <bits/stdc++.h>
const int N = 1e5;
using namespace std;
typedef long long ll;
int n;

struct node{
    ll num,val;
    bool bit[70];
}a[N];

bool cmp(struct node a1,struct node a2)
{
    return a1.num < a2.num;
}

bool ok(struct node a1,struct node a2) // 判断是否符合题意
{
    for(int i = 0 ; i < 61 ; i ++)
        if(a1.bit[i] && !a2.bit[i])
            return false;
    return true;
}

int main()
{
    cin>>n;
    for(int i = 0 ; i <= n ; i ++)
        memset(a[i].bit,false,sizeof(a[i].bit));
    for(int i = 0 ; i < n ; i ++)
        cin>>a[i].num;
    for(int i = 0 ; i < n ; i ++)
        cin>>a[i].val;
    for(int i = 0 ; i < n ; i ++)
        for(int j=0;j<61;j++)  // 取出每一位
            a[i].bit[j] = a[i].num>>j&1;
    sort(a,a+n,cmp);
    vector<struct node> nums;
    nums.clear();
    int flag = 0;
    for(int i = 1 ; i < n ; i ++) // 找重复元素
        if(a[i].num == a[i-1].num)
            nums.push_back(a[i]);
    ll ans = 0;
    for(int i = 0 ; i < n ; i ++) // 暴力比对
    {
        for(int j = 0 ; j < nums.size() ; j ++)
            if(ok(a[i],nums[j]))
            {
                ans += a[i].val;
                break;
            }
    }
    cout<<ans<<endl;
    return 0;
}

再贴一个别人的优秀代码:

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
const int maxn = 7e3+10;
ll a[maxn], b[maxn];
map<ll, ll> msk;
map<ll, ll>msk3;
map<ll, ll> msk2;
vector<ll> vec;
 
int main(){
    int n;
    cin>>n;
    for(int i = 0; i < n; i++){
        cin>>a[i];
        msk[a[i]]++;
        if(msk[a[i]]>=2 && !msk2[a[i]]){
            vec.emplace_back(a[i]);
            msk2[a[i]]++;
        }
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
        cin>>b[i];
    }
    for(auto it : vec){
        for(int i = 0; i < n; i++){
            if((a[i]|it) == it && !msk3[i]){
                msk3[i]++;
                ans+=b[i];
            }
        }
    }
    cout<<ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值