Sequence(priority_queue)
这题很智慧。
VJ上4000多ms
#include<cstdio>
#include<algorithm>
#include<queue>
#include <stdio.h>
#include <vector>
using namespace std;
int main()
{
int T,n,m,sum;
int a[];
scanf("%d",&T);
while(T--)
{
priority_queue<int ,vector<int>, greater<int> >q;
priority_queue<int ,vector<int>, less<int> >p;
scanf("%d%d",&n,&m);
for(int i=; i<m; i++)
{
scanf("%d",&a[i]);
q.push(a[i]);
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
scanf("%d",&a[j]);
while(!q.empty())
{
sum=q.top();
q.pop();
for(int j=; j<m; j++)
{
if(p.size()==m&&p.top()>sum+a[j])
{
p.pop();
p.push(sum+a[j]);
}
else if(p.size()<m)
{
p.push(sum+a[j]);
}
}
}
while(!p.empty())
{
sum=p.top();
p.pop();
q.push(sum);
}
}
int flag=;
while(!q.empty())
{
sum=q.top();
q.pop();
if(flag==)
{
printf(" %d",sum);
}
else
{
printf("%d",sum);
flag=;
}
}
printf("\n");
}
return ;
}
大神的代码(VJ上才刚900ms)
#include <iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<set>
#include<string>
using namespace std;
int main()
{
priority_queue<int,vector<int>,less<int> >p;
int n,m,i,j,a[],b[],t,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=; i<m; i++)
scanf("%d",&a[i]);
sort(a,a+m);
for(k=; k<n; k++)
{
for(i=; i<m; i++)
{
scanf("%d",&b[i]);
p.push(a[]+b[i]);
}
sort(b,b+m);
for(i=; i<m; i++)
{
for(j=; j<m; j++)
{
if(a[i]+b[j]>p.top())
break;
p.pop();
p.push(a[i]+b[j]);
}
}
for(i=; i<m; i++)
{
a[m-i-]=p.top();
p.pop();
}
}
for(i=; i<m-; i++)
printf("%d ",a[i]);
printf("%d\n",a[m-]);
}
}
Sequence(priority_queue)的更多相关文章
- STL之容器适配器priority_queue
priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- STL--容器适配器(queue、priority_queue、stack)
适配器(Adaptor)是提供接口映射的模板类.适配器基于其他类来实现新的功能,成员函数可以被添加.隐藏,也可合并以得到新的功能. STL提供了三个容器适配器:queue.priority_queue ...
- POJ 2442 Sequence
Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于 a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...
- Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏
Sequence Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8277 Accepted: 2708 Description ...
- Sequence《优先队列》
Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...
- STL 之 queue、priority_queue 源代码剖析
/* * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute an ...
- STL--F - Sequence(n*m->之前的最低要求m个月)
F - Sequence Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 2442 - Sequence - [小顶堆][优先队列]
题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...
随机推荐
- Puppet nginx+passenger模式配置
Puppet nginx+passenger模式配置 一.简述:Puppet 运行在单台服务器上默认启动的是一个puppetmaster进程,当遇到client高并发的请求时,基于ruby的WEBRi ...
- 【laravel5.6】laravel 自定义公共函数
1.在 app/Helpers/ 新建一个文件 functions.php,当然这个文件位置和名称你可以自己定义,创建一些函数用于全局调用: 2.在composer.json中的autoload下增加 ...
- 【linux系列】压缩和解压缩tar
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- Github for Windows 登录时报代理问题?
Github for Windows 登录时报如下错误: 不要被它的提示信息误导了. 登录失败,跟代理半毛钱关系都没有. 是 .net framework 组件 的问题. 更新下 .net frame ...
- Maven —— scope 元素的值及其含义
1.compile 缺省值,所属依赖在所有的classpath中可用,同时它们也会被打包(随着项目一起发布). 2.provided 只有当JDK或者某个容器已提供该依赖之后才使用.如servlet. ...
- Android M App休眠 (adb shell dumpsys usagestats)
App休眠 在 Marshmallow 系统,Google 宣布了一个新的功能叫 App 休眠.App 休眠会阻止那些不 常用的 App(几天没有用过的 App)连接网络或者是运行任何程序直至设备充电 ...
- vsCode_1.27.2
User Settings: 一,当前行高亮显示: "editor.renderLineHighlight": "line", 二,如何呈现空白字符(一般选no ...
- [转]OpenStack Neutron运行机制解析概要
转载自:http://panpei.net.cn/2013/12/04/openstack-neutron-mechanism-introduce/ 自从开学以来,玩OpenStack也已经3个月了, ...
- Facebook Cache Token Issue
https://developers.facebook.com/docs/ios/token-caching-ios-sdk/ What's session? https://developers.f ...
- 【BZOJ1004】[HNOI2008]Cards Burnside引理
[BZOJ1004][HNOI2008]Cards 题意:把$n$张牌染成$a,b,c$,3种颜色.其中颜色为$a,b,c$的牌的数量分别为$sa,sb,sc$.并且给出$m$个置换,保证这$m$个置 ...