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 ...
随机推荐
- nginx 代理概念理解
学习nginx http://www.nginx.cn/nginx-how-to nginx官方定义: Nginx 是一个高性能的 Web 和反向代理服务器. 首先明确一个概念 反向代理 反向代理(R ...
- mysql 添加定时任务
之前定时任务都是用quartz 或者spring的任务调度来做的,易于管理,但是要写代码加 配置,其实mysql 自带了job ,先创建一个存储过程
- PHP安全编程:HTTP请求欺骗(转)
一个比欺骗表单更高级和复杂的攻击方式是HTTP请求欺骗.这给了攻击者完全的控制权与灵活性,它进一步证明了不能盲目信任用户提交的任何数据. 为了演示这是如何进行的,请看下面位于http://exampl ...
- 动画Rig设置为Legacy
The animation state AimUp could not be played because it couldn't be found!Please attach an animatio ...
- ubuntu 修改运行级别
只转载了成功的, 具体参见原文 http://www.2cto.com/os/201308/237632.html 第一种方法:(内核级别的) Sudo vi /etc/default/grub ...
- iOS图片拉伸
常用的图片拉伸场景有:聊天页面的气泡,需要根据内容拉伸,但圆角拉伸后会变形,为避免圆角拉伸,可以指定拉伸区域.UIImage实体调用以下方法即可指定拉伸区域. - (UIImage *)stretch ...
- 5事件DOM零级事件跟DOM二级事件
事件的行为传播,行为本身跟事件绑定没有关系:1.全新认识事件(某一个具体的行为)->行为本身:浏览器天生自带的一些行为操作->click,mouseover(mouseenter),mou ...
- Linux sed命令删除指定行
一.删除包含匹配字符串的行## 删除包含baidu.com的所有行sed -i '/baidu.com/d' domain.file 二.删除匹配行及后所有行## 删除匹配20160229的行及后面所 ...
- iOS中使用UIWebView与JS进行交互
iOS中使用UIWebView与JS进行交互 前一段忙着面试和复习,这两天终于考完试了,下学期的实习也有了着落,把最近学的东西更新一下,首先是使用UIWebView与JS进行交互 在webView中我 ...
- UITabBarController自定义二之xib
UITabBarController自定义二之xib 新建一个xib文件 在UITabBarController的子类方法viewDidLoad方法中加载xib 1.-(void)viewDidLoa ...