【枚举+贪心】【TOJ3981】【ICPC Balloons】
给你N种不同颜色气球,每种气球有个数目 count[i],给的同种颜色气球可能是L尺寸,或M尺寸。
M个问题,每个问题有个解决人数ac[i]。
每个问题 要分配一种颜色的气球,尺寸要一样
现在 这些气球可能不符合条件 但是可以将一种气球颜色 刷成另一种(不管之前有没有),但是改变不了尺寸,问最少改变多少个气球颜色。
N《50 M《15;
枚举 2^M 次 考虑 某个问题是用L尺寸的还是M尺寸的
接下来就跟L,M无关了
然后贪心
大的一定先分配,分配完后 利用sum结算要凃的气球颜色数目,显然数量大的具有明显的优势。
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 10000000
using namespace std;
struct ball
{
int size;
int num;
}A[100];
int maxac[100];
int n,m;
int tot=0;
int a[100];
int ans=10000000;
bool cmp(ball a,ball b)
{
if(a.size==b.size)
{
return a.num<b.num;
}
else return a.size<b.size;
}
bool cmp2(int a,int b)
{
return a>b;
}
void input()
{
memset(a,0,sizeof(a));
ans=10000000;
memset(maxac,0,sizeof(maxac));
memset(A,0,sizeof(A));
tot=0;
char c;
cin>>n>>m;
for(int i=1;i<=n;i++)
scanf("%d",&A[i].num);
getchar();
for(int i=1;i<=n;i++)
{
scanf("%c",&c);
if(c=='L') A[i].size=1;
else A[i].size=0,tot++;
}
for(int i=1;i<=m;i++)
scanf("%d",&maxac[i]);
sort(A+1,A+n+1,cmp);
// sort(maxac+1,maxac+m+1,cmp2);
}
int getans(int t)
{
int K[100],T[100];
int R=0;
int TOT=0,TTOT=0,sum1=0,sum2=0;
for(int i=1;i<=n;i++)
{
if(A[i].size==t)
{
K[++TOT]=A[i].num;
sum1+=A[i].num;
}
}
sort(K+1,K+TOT+1,cmp2);
for(int i=1;i<=m;i++)
{
if(a[i]==t)
{
T[++TTOT]=maxac[i];
sum2+=maxac[i];
}
}
sort(T+1,T+TTOT+1,cmp2);
if(sum1<sum2) return oo;
int pp1=0,pp2=0;
for(int i=1;i<=TOT&&i<=TTOT;i++)
{
if(K[i]<=T[i])
{
pp1+=K[i];
}
else pp1+=T[i];
}
return sum2-pp1;
}
void dfs(int p)
{
if(p==m+1)
{
int p[4];
for(int i=0;i<=1;i++)
p[i]=getans(i);
if(p[1]+p[0]<ans) ans=p[1]+p[0];
return ;
}
else
{
a[p]=1;
dfs(p+1);
a[p]=0;
dfs(p+1);
}
}
void solve()
{
ans=oo;
dfs(1);
if(ans==10000000) printf("-1\n");
else printf("%d\n",ans);
}
int main()
{
// freopen("a.in","r",stdin);
// printf("%I64d",a==1>>3*234%333^2*234<<2);
int T;cin>>T;
while(T--)
{
input();
solve();
}
}
【枚举+贪心】【TOJ3981】【ICPC Balloons】的更多相关文章
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
- 枚举+贪心 HDOJ 4932 Miaomiao's Geometry
题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...
- [BZOJ 1028] [JSOI2007] 麻将 【枚举+贪心判断】
题目链接:BZOJ - 1028 题目分析 枚举听的是哪种牌,再枚举成对的是哪种牌,再贪心判断: 从1到n枚举每一种牌,如果这种牌的个数小于0,就返回不合法. 将这种牌的张数 % 3, 剩下的只能和 ...
- 【枚举+贪心】【ZOJ3715】【Kindergarten Electiond】
题目大意: n 个人 在选取班长 1号十分想当班长,他已经知道其他人选择了谁,但他可以贿赂其他人改选他,问贿赂的最小值 ps.他自己也要投一个人 要处理一个问题是,他自己投谁 其实这个问题在这种局面下 ...
- FZU 2252 Yu-Gi-Oh!(枚举+贪心)
Problem 2252 Yu-Gi-Oh! Accept: 105 Submit: 628 Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- UVALive 6912 Prime Switch 暴力枚举+贪心
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- bzoj1050[HAOI2006]旅行comf(枚举+贪心+并查集)
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...
- AtCoder - 2565 枚举+贪心
There is a bar of chocolate with a height of H blocks and a width of W blocks. Snuke is dividing thi ...
随机推荐
- Struts2(一)——总体介绍
这篇博客开始将总结一下有关框架的知识,在开发中合适的利用框架会使我们的开发效率大大提高.当今比较流行的开源框架: 关注数据流程的MVC框架 (Struts1/2, WebWork, Spring MV ...
- ChromiumFX中js调用C#方法
server端代码: ChromiumWebBrowser wb; wb.AddGlobalJSFunction("CfxHelloWorld").Execute += CfxHe ...
- 【HeadFirst 设计模式总结】2 观察者模式
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.我们需要理解报社.订阅系统和订报人之间的关系,订报人通过订阅系统订报,一旦报社有新的报纸,订阅系统就会派人送 ...
- union以及一些扩展
select name,age from Students where Age<3unionselect name ,age from Students where Age >4--两个结 ...
- mvc 微软票据验证
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 对获取config文件的appSettings节点简单封装
转:http://www.cnblogs.com/marvin/archive/2011/07/29/EfficiencyAppSetting.html C#的开发中,无论你是winform开发还是w ...
- G - 密码 kmp、贪心、manachar等等等等
G - 密码 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit Status ...
- 利用ajax做的柱状图,线性统计图,饼状图
柱状图,两个不同类型的数据 以下是html页面代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- kettle新建资源库出错
之前在本地测试新建kettle资源库很顺利,但是在把本地数据迁移到服务器的过程中出现了问题,多次新建资源库失败,提示插入数据错误. 解决办法: 将要执行的sql语句复制出来,单独在Navicat下执行 ...
- wxwidgets demo
环境说明: IOS: mac 10.9 wx: wxWidgets-2.9.5 (http://www.wxwidgets.org/downloads/) gcc: $gcci686-apple- ...