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 ...
随机推荐
- Rank() 、DENSE_RANK()、NTILE(n)的用法-转
Rank() over()/DENSE_RANK() over()的用法 1.Rank() over()/DENSE_RANK() over() 这两个函数与ROW_NUMBER()函数类似,因为 ...
- AngularJS学习笔记
一.初识AngularJS:1.Angularjs通过创建实时模板来代替视图,而不是将数据合并进模板后更新DOM,任何一个独立视图组件中的值都是动态替换的. 二.数据绑定和第一个AngularJS W ...
- java selenium (二) 环境搭建方法一
webdriver 就是selenium 2. webdriver 是一款优秀的,开源的,自动化测试框架. 支持很多语言. 本文描述的是用java Eclipse 如何搭建环境 阅读目录 ...
- NSDate 时间
NSDate *date=[NSDate date]; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; formatter.date ...
- Visual Studio 2015 新建MVC项目 Package Manager Console不能使用 (HRESULT: 0x80131500)
Visual studio 2015 突然新建不了MVC项目,报出错误: HRESULT: 0x80131500 在折腾了很长时间,最后在Github上看到这样一个贴 地址:https://githu ...
- LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2
https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...
- 三角形-css
/*箭头向上*/ .arrow-up { width:; height:; border-left:30px solid transparent; border-right:30px solid tr ...
- java 通过反射获取调用类方法及属性
首先说下反射是什么?反射是Sun公司推出的一组API,此组API位于Java.lang.reflect中 反射的作用是编写工具(例如eclipse),编写框架,当然对于一般的程序,我们不可能用反射来做 ...
- wordpress 获取特色图片url方法
制作主题是需要获取特色图片,直接获取到url能更好的编辑css样式 <?php $large_image_url = wp_get_attachment_image_src( get_post_ ...
- Volley之 JsonRequest 解析JSON 数据
ReqestQueue 和 JsonRequest String jsonUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=63.22 ...