给你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】的更多相关文章

  1. 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 ...

  2. 51nod1625(枚举&贪心)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...

  3. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  4. [BZOJ 1028] [JSOI2007] 麻将 【枚举+贪心判断】

    题目链接:BZOJ - 1028 题目分析 枚举听的是哪种牌,再枚举成对的是哪种牌,再贪心判断: 从1到n枚举每一种牌,如果这种牌的个数小于0,就返回不合法. 将这种牌的张数 % 3, 剩下的只能和 ...

  5. 【枚举+贪心】【ZOJ3715】【Kindergarten Electiond】

    题目大意: n 个人 在选取班长 1号十分想当班长,他已经知道其他人选择了谁,但他可以贿赂其他人改选他,问贿赂的最小值 ps.他自己也要投一个人 要处理一个问题是,他自己投谁 其实这个问题在这种局面下 ...

  6. FZU 2252 Yu-Gi-Oh!(枚举+贪心)

    Problem 2252 Yu-Gi-Oh! Accept: 105    Submit: 628 Time Limit: 1000 mSec    Memory Limit : 32768 KB   ...

  7. UVALive 6912 Prime Switch 暴力枚举+贪心

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  8. bzoj1050[HAOI2006]旅行comf(枚举+贪心+并查集)

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

  9. AtCoder - 2565 枚举+贪心

    There is a bar of chocolate with a height of H blocks and a width of W blocks. Snuke is dividing thi ...

随机推荐

  1. SQL注入原理深度解析

    本文转自:http://www.iii-soft.com/forum.php?mod=viewthread&tid=1613&extra=page%3D1 对于Web应用来说,注射式攻 ...

  2. 细说php(三) 运算符与表达式

    一.算术运算符    +   -  *  /  %  ++  -- <?php $a = 100; $a++; // $a=$a+1; 先用变量再自增1 ++$a; // $a=$a+1; 先用 ...

  3. LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

    原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...

  4. 牵一发动全身【Nhibernate基本映射】

    用牵一发动全身来形容Nhibernate的映射,一点都不夸张.小小的属性的修改,决定了整个Nhibernate的执行动态.以下让我们来详细了解一下,通过回想我们在上篇文章中用到的配置文件,做一个对xm ...

  5. office 文件在网页中显示

    1.如何在网页上显示word和excel a.可以使用office组件或aspose将word 和excel 转换为pdf 然后在网页上打开pdf,但是效果不是很好 .比如说excel 多个工作薄不是 ...

  6. ref 关键字out关键字

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. 统计字符 比如aaabbcca----3a2b1c1a

    package Demo; import java.util.Scanner; /** * Created by chengpeng on 16/11/3. */ /* idea command+al ...

  8. 【转】关于C++程序的编码问题

    引用自:http://blog.chinaunix.net/uid-26790551-id-3190813.html 我们传统的程序基本都只在Windows或只在Linux下运行,Windows程序使 ...

  9. linux的find指令详解。

    引用:http://os.51cto.com/art/200908/141411.htm http://www.oschina.net/translate/15-practical-linux-fin ...

  10. MySQL忘记了密码登录不进去,用命令符修改新的密码重新登录的方法

    MySQL忘记了密码登录不进去,用命令符修改新的密码重新登录的方法: 1.备份my.ini 2.在my.ini字段里 [mysqld] #socket=mysql skip-grant-tables ...