题目连接:http://oj.haihongblog.com/problem.php?id=1003

线段树维护区间最小值,并且求解下标

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std; const int maxn=+ ;
const int INF=0x7FFFFFFF;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int N,K,zzt;
vector<int> ans; int MIN[maxn<<]; void PushUP(int rt)
{
MIN[rt]=min(MIN[rt<<],MIN[rt<<|]);
} void Build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&MIN[rt]);
return;
}
int m=(l+r)>>;
Build(lson);
Build(rson);
PushUP(rt);
} void Update(int p,int add,int l,int r,int rt)
{
if(l==r)
{
MIN[rt]=add;
return;
}
int m=(l+r)>>;
if(p<=m)
Update(p,add,lson);
else
Update(p,add,rson);
PushUP(rt);
} int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r) return MIN[rt];
int m=(l+r)>>;
int ret=INF;
if(L<=m) ret=min(ret,Query(L,R,lson));
if(R>m) ret=min(ret,Query(L,R,rson));
return ret;
} void Find(int L,int R,int l,int r,int rt)
{ if(L<=l&&R>=r)
{
int lx=l,rx=r,node=rt;
while()
{
int mx=(lx+rx)/;
if(lx==rx) {zzt=lx;break;}
if(K>=MIN[*node])
{
node=*node;
rx=mx;
continue;
} if(K>=MIN[*node+])
{
node=*node+;
lx=mx+;
continue;
}
}
return;
} int m=(l+r)>>;
if(K>=MIN[rt<<]&&L<=m) Find(L,R,lson);
if(zzt!=-) return;
if(K>=MIN[rt<<|]&&R>m) Find(L,R,rson);
if(zzt!=-) return;
} int main()
{
// freopen("F:\\test (1).in","r",stdin);
// freopen("F:\\out.txt","w",stdout); while(~scanf("%d%d",&N,&K)){
Build(,N,); bool fail=;
int pos=;
ans.clear(); for(int i=;i<=N;i++)
{
int P=Query(pos,N,,N,);
if(K>=P) //后面一段存在可以吃的点
{
zzt=-; //找到第一个大于等于K的位置 zzt
Find(pos,N,,N,);
ans.push_back(zzt);
pos=zzt;
//该位置的值更新为INF
Update(zzt,INF,,N,);
K++;
}
else //后面一段不存在可以吃的点,从头开始找
{
P=Query(,N,,N,);
if(K>=P)
{
zzt=-; //找到第一个大于等于K的位置 zzt
Find(,N,,N,);
ans.push_back(zzt);
pos=zzt;
//该位置的值更新为INF
Update(zzt,INF,,N,);
K++;
} else //找不到了,失败
{
fail=;
break;
}
}
} if(fail) printf("-1\n");
else
{
for(int i=;i<ans.size();i++)
{
printf("%d",ans[i]);
if(i<ans.size()-) printf(" ");
else printf("\n");
}
}
}
return ;
}

HaiHongOJ 1003 God Wang的更多相关文章

  1. Bestcoder#5 1003

    Bestcoder#5 1003 Poor RukawTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  2. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  3. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

  4. PKU 1003解题

    首先庆祝一下,今天连A了3题.感觉后面这题太简单了.. 由于英文不好 ,找了个翻译: 若将一叠卡片放在一张桌子的边缘,你能放多远?如果你有一张卡片,你最远能达到卡片长度的一半.(我们假定卡片都正放在桌 ...

  5. POJ做题笔记:1000,1004,1003

    1000 A+B Problem 题目大意:输入两个数a和b,输出他们的和. 代码: #include <stdio.h> int main() { int a, b; while (sc ...

  6. 未注册wang域名批量查询工具

    一.支持规则查询 可自定义生成域名进行查询,可生成任意位数的字母数字域名,根据[声母].[韵母]生成单拼,双拼,三拼等域名,还可根据字典生成,支持全拼.首拼识别,全国城市区号.城市全拼.城市首拼.热门 ...

  7. 迪杰斯特拉算法——PAT 1003

    本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...

  8. hdu5024 Wang Xifeng's Little Plot (水

    http://acm.hdu.edu.cn/showproblem.php?pid=5024 网络赛 Wang Xifeng's Little Plot Time Limit: 2000/1000 M ...

  9. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

随机推荐

  1. hdu_5752_Sqrt Bo(xjb搞)

    题目链接:hdu_5752_Sqrt Bo 题意: 给你一个数,问你最少要开多少次方才能为1 题解: 我们发现如果给的数大于232 那么肯定在5次以内是开不出来的,所以直接输出TAT,然后小于的就模拟 ...

  2. 矩阵快速幂——将运算推广到矩阵上HDU 1575

    /* 本题的思路比较简单,就是将递推公式写出来,然后表达成为一个矩阵的形式 最后通过计算就可以得到一个符合题目要求的矩阵, 然后就是将矩阵上面所有的对角线元素相加 得到的结果即为所求的目标 */ #i ...

  3. git简单使用教程

    git 的基本使用指令 我们先来简单熟悉一下 git 的简单使用的指令, 作为最基本的 git 指令一定要熟悉 12345678910111213141516171819202122232425262 ...

  4. 关于用模拟器运行百度地图API无法定位的问题 - 不能用模拟器

    模拟器是没有办法定位,当你加入定位模块的时候,传出的参数都是空的. 定位的这个方法函数,是通过回调接口来实现,而且触发该事件的时候,需要经纬度位置改变.官方文档写得很清楚,简单点来说,就是你没有GPS ...

  5. android view构造函数研究

           上周遇到了SurfaceView的constructor的问题,周末决定略微细致地研究一下这个令人发指的玩意.   SurfaceView是View的子类,与View一样有三个const ...

  6. asp.net html table to DataTable

    添加引用 http://htmlagilitypack.codeplex.com/downloads/get/437941 protected void Export(string content,s ...

  7. Django urls常用匹配语法

    url from django.conf.urls import url from . import views urlpatterns = [ url(r'^articles/2003/$', vi ...

  8. IIS express 7.5 设置默认文档

    在C:\Users\[电脑用户名]\Documents\IISExpress\config 下面有个applicationhost.config文件,打开文件找到<system.webServe ...

  9. Oracle自增长序列

    create table user_info ( id number(6) primary key, username varchar2(30) not null, password varchar2 ...

  10. 初次stack-overflow 提交答案

    初次在stack-overflow上面提交答案,首先编辑器非常好用,语法检查都有, 还有付费版的,更高级,更好用,nice. 付费版:https://www.grammarly.com/upgrade ...