Sequence
http://poj.org/problem?id=2442
用STL写的时间为:5657MS
#include<cstdio>
#include<algorithm>
#include<queue>
#define MAXN 2005
using namespace std;
int main()
{
int t,n,m,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int a[MAXN];
priority_queue< int,vector<int>,greater<int> >q;
priority_queue< int,vector<int>,less<int> >p;
for(int i=; i<m; i++)
{
scanf("%d",&c);
q.push(c);
}
for(int j=; j<n; j++)
{
for(int k=; k<m; k++)
{
scanf("%d",&a[k]);
}
while(!q.empty())
{
int mm=q.top();
q.pop();
for(int h=; h<m; h++)
{
if(p.size()==m&&p.top()>mm+a[h])
{
p.pop();
p.push(mm+a[h]);
}
else if(p.size()<m)
{
p.push(mm+a[h]);
}
}
}
while(!p.empty())
{
q.push(p.top());
p.pop();
}
}
int mark=;
for(int i=;i<m;i++)
{
if(mark)
{
printf("%d",q.top());
mark=;
}
else printf(" %d",q.top());
q.pop();
}
printf("\n");
}
return ;
}
我用堆写的时间:3969MS
#include<cstdio>
#include<algorithm>
#include<queue>
#define MAXN 2005
long long a[],b[];
using namespace std;
int len=,len1=;
void up1(int n)
{
a[++len]=n;
int p=len;
int q=p/;
long long m=a[p];
while(q>=&&m<a[q])
{
a[p]=a[q];
p=q;
q=p/;
}
a[p]=m;
}
void up2(int n)
{
b[++len1]=n;
int p=len1;
int q=p/;
long long m=b[p];
while(q>=&&m>b[q])
{
b[p]=b[q];
p=q;
q=p/;
}
b[p]=m;
}
void down1(int p)
{
a[]=a[len--];
int q=p*;
long long m=a[p];
while(q<=len)
{
if(q<len&&a[q]>a[q+])
q++;
if(a[q]>=m) break;
else
{
a[p]=a[q];
p=q;
q=p*;
}
}
a[p]=m;
}
int pop1()
{
long long r=a[];
return r;
}
int pop2()
{
long long r=b[];
return r;
}
void down2(int p)
{
b[]=b[len1--];
int q=p*;
long long m=b[p];
while(q<=len1)
{
if(q<len1&&b[q]<b[q+])
q++;
if(b[q]<=m) break;
else
{
b[p]=b[q];
p=q;
q=p*;
}
}
b[p]=m;
} int main()
{
int t,n,m,c;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int aa[MAXN];
for(int i=; i<m; i++)
{
scanf("%d",&c);
up1(c);
}
for(int j=; j<n; j++)
{
for(int k=; k<m; k++)
{
scanf("%d",&aa[k]);
}
while(len!=)
{
int mm=pop1();
down1();
for(int h=; h<m; h++)
{
if(len1>=m&&b[]>mm+aa[h])
{
down2();
up2(mm+aa[h]);
}
else if(len1<m)
{
up2(mm+aa[h]);
}
}
}
while(len1!=)
{
up1(b[]);
down2();
}
}
int mark=;
for(int i=;i<m;i++)
{
if(mark)
{
printf("%lld",a[]);
mark=;
}
else printf(" %lld",a[]);
down1();
}
printf("\n");
}
return ;
}
Sequence的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- 一些正则在js使用方法
输入框直接正则判断 <input type="password" name="pwd" placeholder="密码只能以数字\英文\@\.& ...
- windows7实用快捷键 分类: windows常用小技巧 2014-04-18 14:34 169人阅读 评论(0) 收藏
几个比较实用的快捷键 windows键:简写成win win+ 方向键上下,可以使当前窗体放大或缩小 win+ 方向键左右,可以使当前窗体悬靠在左边或右边 win+Home 仅保留当前窗体 ...
- setTimeout的作用以及setTimeout延时0毫秒的作用
以下代码输出的顺序是? console.log(1); setTimeout(function(){ console.log(2); }, 0); setTimeout(function(){ con ...
- 【转】四种常见的POST提交数据方式
HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...
- TCP/IP协议原理与应用笔记08:对等层和对等实体
1. 我们直接通过下面这个图,就可以直观了解: Outlook :收发邮件的软件组件. IE:浏览器. CutFTP:文件传输工具. 小结:因为为了完成不同的功能,所以会出现不同实体,这些不同的实体为 ...
- 500 OOPS: vsftpd: refusing to run with writable root inside chroot()解决方法
vsftpd.conf配置文件如下: [root@rusky ~]# cat /etc/vsftpd/vsftpd.conf | grep -v "#" anonymous_ena ...
- Adobe DreamweaverCS6安装及破解(序列号+破解补丁)
一:安装 1) Adobe DreamweaverCS6中文版下载地址:Adobe DreamweaverCS6中文版下载 2)Adobe DreamweaverCS6安装及破解说明下载地址:Adob ...
- SQL Server主键自动生成_表and存储过程
主键表: CREATE TABLE [dbo].[KEYCODE]( [KeyName] [varchar](12) NOT NULL, [KeyTableName] [varchar](40) NU ...
- Swift - 25 - 控制转移和二维数组
//: Playground - noun: a place where people can play import UIKit // fallthrough // fallthrough会在当前c ...
- XMLHelper 类
这个XMLHelper类中包括了XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现,有兴趣的朋友,可以进来看看,所有代码都在WebForm和WinForm中调试通过. 这是下面 ...