您的位置:首页 > 其它

【贪心】 ZOJ 3908 Number Game

2015-10-11 20:44 288 查看
点击打开链接

题意:给出N 个数 每次可以取两个数  和不超过k ,价值为两个数的积,至多可以取m次,求价值最大

用mulitset 储存 数,然后从最大的开始取 找到符合的最大的数,然后删去这两个数

最后排序加一下

//#include <bits/stdc++.h>
//using namespace std;
//#define lson l, m, rt<<1
//#define rson m+1, r, rt<<1|1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//typedef long long LL;
//typedef unsigned long long ULL;
//const int INF = 1<<29;
//const LL mod = 1e9+7;
//const int MAXN = 1100;//点数的最大值
//const int MAXM = 520;//边数的最大值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <list>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
const int INF = 1<<29;
const LL mod = 530600414;
const int MAXN = 201314+7;
const int MAXM = 21101;

multiset <int> st;
multiset <int> ::iterator it1,it2;
vector<int>ve;
int t,n,m,k,a;
int main()
{
scanf("%d",&t);
while(t--)
{
st.clear();
ve.clear();
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)
{
scanf("%d",&a);
if(a<k)
st.insert(a);
}
LL ans=0;
while(st.size()>=2)
{
it1=st.end();
it1--;
it2=st.lower_bound(k-(*it1));
if(it2==st.end()||it2==it1)
it2--;
int flag=0;

if(it2==st.begin()&&(*it1)+(*it2)>k)//找到begin位置
flag=1;
while((*it1)+(*it2)>k&&!flag)
{
it2--;
if(it2==st.begin()&&(*it1)+(*it2)>k)
flag=1;
}
if(it1==it2)
{
it2--;
}
if(flag)//找不到数
{
st.erase(it1);
continue;
}
ve.push_back((*it1)*(*it2));
st.erase(it1);
st.erase(it2);
}
sort(ve.begin(),ve.end());
for(int i=ve.size()-1;i>=0&&m;i--)
{
m--;
ans+=ve[i];
}
cout<<ans<<endl;
}
return 0;
}
/*
18 1 16
11 46 5 6 17 15 12 5 33 2 24 12 39 2 18 12 25 6
ans:55

17 2 16
25 8 25 22 39 30 25 5 8 25 17 8 12 14 35 39 4
ans:112

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: