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;
}