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 ...
随机推荐
- 【Eclipse】一个简单的 RCP 应用 —— 显示Eclipse 的启动时间。
1 创建一个插件项目 1.1 File - New - Plug-in Project 注: 1 如果 New 下没有 Plug-in Project , 到 Other 里面去找. 2 如上截图的下 ...
- Firefox --- 火狐浏览器下载
http://www.firefox.com.cn/download/
- javah生成jni头文件时报错 Error: cannot access android.support...
javah生成jni头文件时报错: Error: cannot access android.support.v7.app.AppCompatActivity class file for andro ...
- 使用virtualbox 配置 linux host-only虚拟主机连接外网(转载)
host-only 下的虚拟机之间可以互相访问,虚拟机和宿主机可以互相访问,但是虚拟机不能访问外网. 需要设置: 1.宿主机设置 先对宿主机(windows机器,我这里是win7系统)进行相关配置. ...
- Elasticsearch学习之head插件安装
通过elasticseach自带的plugin命令 elasticsearch/bin/plugin -install mobz/elasticsearch-head 如下图: 2. zip包安装 ...
- UVA 1335 Beijing Guards(二分答案)
入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...
- 【咸鱼教程】TextureMerger1.6.6 一:Egret MovieClip的制作和使用
几个月没写代码了.然后突然用到TextureMerger,发现不会用序列图做动画了... 于是写下过程,以防忘记... MovieClip主要是用于游戏中的动画制作和播放.支持swf.gif.序列图等 ...
- shell脚本中对简单实现对log的处理
用shell在写小程序时,log没用像python样用logging模块可以直接使用,下面我们就简单写下用shell函数来实现log分级 #/bin/bash sys_log="/var/l ...
- Unity3D笔记十九 持久化数据
1.PlayerPrefs类(生命周期???) 1.1 保存与读取数据 在C#中类似缓存.Cookie.Session等保存数据的,但是有点区别的是在C#中如果在取值时没有取到默认值则返回值是NULL ...
- java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder
java.lang.NoClassDefFoundError: org/jdom/input/SAXBuilder 少导入了jdom.jar包. 下载地址:http://www.jdom.org/do ...