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 ...
随机推荐
- 【技术分享会】 @第五期 angularjs
前言 AngularJS 最初由Misko Hevery 和Adam Abrons于2009年开发,后来成为了Google公司的项目.AngularJS弥补了HTML在构建应用方面的不足,其通过使用标 ...
- 【技术分享会】 @第四期 JQuery插件
本讲内容 JavaScript JQuery JQuery插件 实例 JavaScript 前端开发工程师必须掌握的三种技能 描述内容的HTML 描述网页样式的CSS 描述网页行为的JavaScrip ...
- margin-left:10px; 不同浏览器距离为什么不一样?
这是一个ie的bug.. 如果你设置margin对象是浮动的.ie就会把你设置的值双倍处理..可以设置浮动对象的 display:inline 解决
- 【Spring源码分析系列】启动component-scan类扫描加载过程
原文地址:http://blog.csdn.net/xieyuooo/article/details/9089441/ 在spring 3.0以上大家都一般会配置一个Servelet,如下所示: &l ...
- CF 1073C Vasya and Robot(二分答案)
C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 2015.7.7js-07-2(基础)
1.用求模来计算时间,秒%60,就能获取剩余的秒 var s = 362; var minute = parseInt(s/60) + "分" //取得分 var second = ...
- ++ fatal error C1083: 无法打开预编译头文件:“.\Debug\router.pch”
一.出现此错误首先检查:stdafx.cpp文件上右键——属性,预编译头选“创建”,其它cpp选“使用”. 二.如果是采用这样的设置,还是有错误,重新生成解决方案,重新调试. 三.实在不行的话,步骤/ ...
- Yet another way to manage your NHibernate ISessionFactory
So here is my current UnitOfWork implementation. This one makes use of the somewhat new current_ses ...
- iOS开发中id、NSObject *、id、instancetype四者有什么区别?
在使用Objective-C语言进行iOS应用开发的时候,常常会涉及到id.NSObject *.id.instancetype这四个概念的使用,但这四者也是iOS初学者最易混淆的内容,下面小编看 ...
- resize 按钮不会被伪元素遮盖
textarea默认有个resize样式,效果就是下面这样 读 <css 揭秘>时发现两个亮点: 其实这个属性不仅适用于 textarea 元素,适用于下面所有元素: elements w ...