codeforces732E Sockets(贪心)
来源:互联网 发布:一键获取淘宝联盟 编辑:程序博客网 时间:2024/06/05 07:54
题意:
给出一些给定功率的电脑和插座,只有功率匹配才能连接,另外有一些适配器,连在插座上可以把插座功率降为1/2,求最多匹配电脑数和需要的适配器数以及具体映射。
要点:
这题就是贪心+STL的应用,主要是贪心的证明,具体证明看下面这个博客:
参考博客:点击打开链接
#include<iostream>#include<map>#include<vector>#include<algorithm>using namespace std;const int maxn = 300000;map<int, int> pre;vector<int> s[maxn];int n, m, cnt;int ans1[maxn], ans2[maxn];struct node{int x, pos;}a[maxn];bool cmp(node a, node b){return a.x < b.x;}int main(){int i, x;scanf("%d%d", &n, &m);for (i = 1; i <= n; i++){scanf("%d", &x);int temp = pre[x];//pre数组把相同功率不同下标的电脑指向同一个位置if (temp == 0){pre[x] = ++cnt;s[cnt].push_back(i);//s提供相同功率的所有下标的电脑}elses[temp].push_back(i);}for (i = 1; i <= m; i++){scanf("%d", &a[i].x);a[i].pos = i;}sort(a + 1, a + 1 + m, cmp);int total = 0,num=0;for (i = 1; i <= m; i++){int sub = 0;x = a[i].x;while (x){int xx = pre[x];if (xx){int len = s[xx].size();if (len){int it = s[xx][len - 1];ans2[it] = a[i].pos;ans1[a[i].pos] = sub;s[xx].pop_back();total += sub;num++;break;}elsepre.erase(x);}if (x == 1)break;x = (x + 1) >> 1;sub++;}}printf("%d %d\n", num, total);for (i = 1; i <= m; i++)printf("%d%c", ans1[i], i == m ? '\n' : ' ');for (i = 1; i <= n; i++)printf("%d%c", ans2[i], i == n ? '\n' : ' ');return 0;}
0 0
- codeforces732E Sockets(贪心)
- codeforces732E Sockets(数据结构+贪心+思维)
- Codeforces 732E Sockets【贪心】
- Gym 100886J Sockets(二分+贪心)
- 贪心,优化(Sockets,cf 732e)
- codeforce 732e Sockets (贪心)
- CodeForces 732E Sockets 【贪心】【STL】
- CodeForces 732 E.Sockets(贪心)
- sockets
- sockets
- Sockets
- Sockets
- 【CF 732E】Sockets(优先队列+贪心)
- sockets-network sockets (AFX_INET)
- 网络程序设计 Sockets
- Windows Sockets
- Windows Sockets
- 网络程序设计 Sockets
- 数据库(3):MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
- leet code - Third Maximum Number
- hdu 2065 指数型生成函数
- html5笔记之classList
- [整理]VC编译程序时出现the source files are both configured to produce the output file....
- codeforces732E Sockets(贪心)
- C++虚函数与纯虚函数
- 重建二叉树
- 关于浮动知识的总结
- C++ try catch的问题
- 716a
- 94. Binary Tree Inorder Traversal
- 10月再见
- 在Ajax中使用get和post所遇到的问题及解决办法