codeforces 732E(贪心)
题目链接:http://codeforces.com/contest/732/problem/E
题意:有n台计算机,m个插座,每台计算机有一个值a[i],每个插座有一个值b[i],每个插座最多只能对应一台计算机,且只有a[i] == b[j]时才能配对。现有无限台适配器,适配器能使b[i]减半,求最多能使多少计算机与插座配对(c),以及对应的最小的适配器个数(u)(即先考虑c最大,再使u最小)。
思路:先对计算机和插座按值排序,不断减半,发现能配对就配对。
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + ;
int a[N],b[N],c,u;
struct node
{
int val,id;
node(){}
node(int v,int i) : val(v),id(i) {}
bool operator < (const node &t)
{
if(val != t.val)
return val < t.val;
return id < t.id;
}
}p[N],s[N];
map <int,int> mp;//计算机值出现的次数
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i = ; i <= n; i++)
{
scanf("%d",&p[i].val);
p[i].id = i;
mp[p[i].val]++;
}
sort(p + , p + n + );
for(int i = ; i <= m; i++)
{
scanf("%d",&s[i].val);
s[i].id = i;
}
sort(s + , s + m + );
for(int i = ; i <= m; i++)
{
for(int j = ; j < ; j++)
{
if(mp.count(s[i].val))//发现可能匹配
{
int t = lower_bound(p + , p + n + , node(s[i].val,)) - p + mp[s[i].val] - ;//找到位置(由于有可能有多个相同的值,这里先从最后一个开始往前推)
if(p[t].val == s[i].val && !b[p[t].id])//值相等 && 该计算机还未匹配
{
b[p[t].id] = s[i].id;
mp[s[i].val]--;//数量-1
a[s[i].id] = j;
c++;
u += j;
break;
}
}
s[i].val = (s[i].val + ) >> ;
}
}
printf("%d %d\n",c,u);
for(int i = ; i <= m; i++)
printf("%d ",a[i]);
printf("\n");
for(int i = ; i <= n; i++)
printf("%d ",b[i]);
return ;
}
codeforces 732E(贪心)的更多相关文章
- Codeforces 732e [贪心][stl乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
- Codeforces 721D [贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...
- CodeForces - 424B (贪心算法)
Megacity Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
随机推荐
- applicationContext配置文件中的属性说明
lazy-init:设置只对scop属性为singleton的bean起作用. 1.true:延迟加载:这时在第一次向容器通过getBean索取bean时实例化的. 2.false:表示spring启 ...
- 为什么.Net要求序列化的类必须有一个无参数的构造函数
刚才用xml序列化器,序列化一个类,结果报错说序列化的类必须带有一个无参的构造函数,好奇怪啊.为什么要有这么苛刻的条件,而且xml序列化还要求序列化的成员是public. 我以前一直觉得序列化器是一个 ...
- category - junit用例分组执行
一.category 和 testSuite的比较 (1)testSuite是类级分组(xx.class) (2)category是用例级分组(@Test) (3)category是testSuite ...
- guava学习--FutureFallback
FutureFallback提供一个Future的备用来替代之前失败的Future,常被用来作为Future的备份或者默认的值. @Testpublic void testFuturesFallbac ...
- JQuery,拼接字符串问题(求助)
Js代码 $("#span_btnSave").html(str1); 结果 <span id="span_btnSave"><button ...
- CodeForces #362 div2 B. Barnicle
题目链接: B. Barnicle 题意:给出科学计数法 转化成十进制的整数或小数 并输出. 思路:暑假训练赛见过了,当时大腿A掉了,并表示是道水题. 刷CF再次遇见,毫不留情WA了几次.比如: 0. ...
- 401. Binary Watch
[题目] Total Accepted: 10330 Total Submissions: 24061 Difficulty: Easy Contributors: Admin A binary wa ...
- 为bootstrap添加更多自定义图标
From: http://blog.csdn.net/mengxiangfeiyang/article/details/45224731 Twitter Bootstrap 真是前端开发的瑞士军刀,作 ...
- 用JS获取地址栏参数的方法(超级简单)
方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new RegExp("( ...
- 常用OpenLDAP命令
ldappasswd -x -D "cn=Manager,dc=clouderachina,dc=com" -W "uid=mis,ou=Group,dc=clouder ...