您的位置:首页 > 其它

CodeForces - 808F Card Game (随机化大法)

2017-05-23 14:36 447 查看
F. Card Game

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Digital collectible card games have become very popular recently. So Vova decided to try one of these.

Vova has n cards in his collection. Each of these cards is characterised by its power pi,
magic number ci and
level li.
Vova wants to build a deck with total power not less than k, but magic numbers may not allow him to do so — Vova can't place two cards
in a deck if the sum of the magic numbers written on these cards is a prime number. Also Vova cannot use a card if its level is greater than the level of Vova's character.

At the moment Vova's character's level is 1. Help Vova to determine the minimum level he needs to reach in order to build a deck with the required
total power.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100000).

Then n lines follow, each of these lines contains three numbers that represent the corresponding card: pi, ci and li (1 ≤ pi ≤ 1000,1 ≤ ci ≤ 100000, 1 ≤ li ≤ n).

Output

If Vova won't be able to build a deck with required power, print  - 1. Otherwise print the minimum level Vova has to reach in order to build
a deck.

Examples

input
5 8
5 5 1
1 5 4
4 6 3
1 12 4
3 12 1


output
4


input
3 7
4 4 1
5 8 2
5 3 3


output
2


题意:给你一列数,有三个属性,等级,价值,特征值,从中选出一些数,使这些数的价值和不低于
给定值,并且任意两个数的特征值相加不为素数,求这些满足要求的最小等级
解:因为不知道相斥的俩个数留哪一个数,所以循环随机M次数组,把答案搜出来

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5+10;
const int M = 3069;
int num[4];
int prime
, a
, lx
, b
;
struct node
{
int v, c, level;
} p
;
int cmp(node A,node B)
{
return A.level<B.level;
}
int n, k;

int main()
{
memset(a,0,sizeof(a));
int cnt=0;
a[0]=a[1]=1;
for(int i=0; i<N; i++)
{
if(a[i]) continue;
for(int j=2*i; j<N; j+=i)  a[j]=1;
}
scanf("%d %d", &n, &k);
for(int i=1; i<=n; i++)   scanf("%d %d %d", &p[i].v,&p[i].c,&p[i].level);
sort(p+1,p+n+1,cmp);
memset(lx,0,sizeof(lx));
for(int i=1; i<=n; i++)   lx[p[i].level]=i;
int ans=0, sum, c;
for(int i=1; i<=n; i++)
{
if(!lx[i]) continue;
if(i==4)
{
int l;
l=0;
}
for(int h=0; h<M; h++)
{
random_shuffle(p+1,p+lx[i]+1);
c=0, sum=0;
for(int j=1; j<=lx[i]; j++)
{
int flag=0;
for(int q=0;q<c;q++)
{
if(!a[p[j].c+p[b[q]].c])
{
flag=1;
break;
}
}
if(!flag)
{
b[c++]=j;
sum+=p[j].v;
}
}
if(sum>=k)
{
printf("%d\n",i);
return 0;
}
}

}
printf("-1\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: