【贪心】 ZOJ 3908 Number Game

来源:互联网 发布:华为云计算部门效益 编辑:程序博客网 时间:2024/05/17 18:46

点击打开链接

题意:给出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 1611 46 5 6 17 15 12 5 33 2 24 12 39 2 18 12 25 6ans:5517 2 1625 8 25 22 39 30 25 5 8 25 17 8 12 14 35 39 4ans:112*/


0 0
原创粉丝点击