【枚举+贪心】【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 ...
随机推荐
- MD5加密算法(转)
获取字符串的MD5摘要 原文更详细: http://www.weixuehao.com/archives/474 代码如下: import java.security.MessageDigest; p ...
- log4net 使用与配置 每天一份log文件
1.下载 或 在nuget安装 log4net 2. web.config (app.config) <configuration> <configSections> < ...
- Elasticlunr.js 简单介绍
Elasticlunr.js 项目地址:http://elasticlunr.com/ 代码地址:https://github.com/weixsong/elasticlunr.js 文档地址:htt ...
- 基于 koajs 的前后端分离实践
一.什么是前后端分离? 前后端分离的概念和优势在这里不再赘述,有兴趣的同学可以看各个前辈们一系列总结和讨论: 系列文章:前后端分离的思考与实践(1-6) slider: 淘宝前后端分离实践 知乎提问: ...
- RMAN数据库恢复 之归档模式有(无)备份-丢失数据文件的恢复
1.归档模式有备份,丢失数据文件的恢复归档模式有备份,不管丢失什么数据文件,直接在RMAN下RESTOER--->RECOVER--->OPEN即可. RMAN> STARUP MO ...
- Tomcat 的三种(bio,nio.apr) 高级 Connector 运行模式
tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态. ...
- PHP常用内置函数
$_SERVER['SCRIPT_NAME'] 返回/mantis/test.php 相对路径 __FILE__返回文件的绝对路径 $_SERVER['HTTP_X_FORWARDED_PROTO'] ...
- ios app相互调用
被调用app配置 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NS ...
- struts1:Struts的中央控制器
在Struts框架中,有多个组件分担控制器的工作,它们分别是ActionServlet类,RequestProcessor类和Action类等,其中ActionServlet是Struts框架中的核心 ...
- 同一个View双击事件&&单击事件
final long[] mHits = new long[2]; // iv_flaw_flow.setOnClickListener(new View.OnClickListener() { // ...