就是从n位数中取出n-d个数字按顺序排成一排组成一个新数使得其最大

算法:

从前往后确定每一位。找第i位时,要求后面留下d-i位的空间,

因此第i位应该从第i-1位原来位置+1到第d+i位寻找

用线段树确定区间最大值(其实直接用优先队列就行了,可能会多一些多余的出队操作)

更好的算法:

***引用

后来看到一个博客写的特别巧妙,

每读取一个字符,如果ans中有字符,且如果删除一个字符后面的数字数量依然够的话,

并且ans中最后一个数字比新读取的小,那么删除最后一个字符,把新读取的字符加入ans.

http://www.cnblogs.com/jihe/p/4883573.html***

#include<cstdio>
#include<algorithm>
using namespace std;
int c[300010];
int ans[300010];
int n,d;
struct Node
{
int lc,rc,l,r,maxpos;
}node[1000010];
int num_node;
int getnode()
{
return num_node++;
}
int build(int l,int r)
{
int _tmp=getnode();
node[_tmp].l=l;
node[_tmp].r=r;
if(l==r)
{
node[_tmp].maxpos=l;
return _tmp;
}
int m=(l+r)>>1;
node[_tmp].lc=build(l,m);
node[_tmp].rc=build(m+1,r);
if(c[node[node[_tmp].lc].maxpos]<c[node[node[_tmp].rc].maxpos])
node[_tmp].maxpos=node[node[_tmp].rc].maxpos;
else
node[_tmp].maxpos=node[node[_tmp].lc].maxpos;
return _tmp;
}
int query(int L,int R,int nownode)
{
int l=node[nownode].l;
int r=node[nownode].r;
if(L<=l&&r<=R)
return node[nownode].maxpos;
int t,ans=n+1,m=(l+r)>>1;
if(L<=m)
{
t=query(L,R,node[nownode].lc);
if(c[ans]<c[t])
ans=t;
}
if(R>m)
{
t=query(L,R,node[nownode].rc);
if(c[ans]<c[t])
ans=t;
}
return ans;
}
int main()
{
int i,t;
scanf("%d%d",&n,&d);
while(n!=0&&d!=0)
{
num_node=0;
t=0;
for(i=1;i<=n;i++)
scanf("%1d",&c[i]);
c[n+1]=-1;
build(1,n);
for(i=1;i<=n-d;i++)
{
t=query(t+1,d+i,0);
printf("%d",c[t]);
}
printf("\n");
scanf("%d%d",&n,&d);
}
return 0;
}

最新文章

  1. Android onActivityResult没响应
  2. ajax跨域之设置Access-Control-Allow-Origin
  3. 【leetcode】Anagrams (middle)
  4. java mail(发送邮件--163邮箱)
  5. Linux环境PHP7.0安装
  6. MySQL(20):事务简介 和 事务的四个特性
  7. BindingFlags说明
  8. Airbnb创始人:屌丝的逆袭之路
  9. 彻底弄清c标准库中string.h里的常用函数用法
  10. nginx 开展对RT5350
  11. DevOps之服务器
  12. json的命名空间
  13. TOTP 介绍及基于C#的简单实现
  14. C语言面试题分类-&gt;位运算
  15. python全栈开发day44-js、DOM、BOM
  16. [转载]AngularJS学习笔记
  17. vue-router &quot;path&quot; is required in a route configuration
  18. 使用lets encrypt证书加密
  19. python安装方法
  20. 使用history.pushState()和popstate事件实现AJAX的前进、后退功能

热门文章

  1. ajax返回页面停留跳转
  2. 赵雅智_SimpleCursorAdapter
  3. Android cookies正确的更新方式
  4. maven统一配置
  5. springboot 多数据源(三种数据库连接池--JDBC,dbcp2,Druid)
  6. codeforces 679e Bear and Bad Powers of 42
  7. YTU 2481: 01字串
  8. codeforces B. Sereja and Mirroring 解题报告
  9. 一步一步学Silverlight 2系列(20):如何在Silverlight中与HTML DOM交互(下)
  10. Android自定义控件实现带有清除按钮的EditText