zoukankan      html  css  js  c++  java
  • 洛谷 P1052 过河

    题目描述

    在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧。在桥上有一些石子,青蛙很讨厌踩在这些石子上。由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上的一串整点:0,1,……,L(其中L是桥的长度)。坐标为0的点表示桥的起点,坐标为L的点表示桥的终点。青蛙从桥的起点开始,不停的向终点方向跳跃。一次跳跃的距离是S到T之间的任意正整数(包括S,T)。当青蛙跳到或跳过坐标为L的点时,就算青蛙已经跳出了独木桥。

    题目给出独木桥的长度L,青蛙跳跃的距离范围S,T,桥上石子的位置。你的任务是确定青蛙要想过河,最少需要踩到的石子数。

    输入输出格式

    输入格式:

    输入文件river.in的第一行有一个正整数L(1 <= L <= 10^9),表示独木桥的长度。第二行有三个正整数S,T,M,分别表示青蛙一次跳跃的最小距离,最大距离,及桥上石子的个数,其中1 <= S <= T <= 10,1 <= M <= 100。第三行有M个不同的正整数分别表示这M个石子在数轴上的位置(数据保证桥的起点和终点处没有石子)。所有相邻的整数之间用一个空格隔开。

    输出格式:

    输出文件river.out只包括一个整数,表示青蛙过河最少需要踩到的石子数。

    输入输出样例

    输入样例#1:
    10
    2 3 5
    2 3 5 6 7
    
    输出样例#1:
    2

    说明

    对于30%的数据,L <= 10000;

    对于全部的数据,L <= 109。

    2005提高组第二题

    首先呢,如果数据没那么大,那么就是一个很简单的dp,可是实际上10^9次方确实很大,于是观察其他数据,可以发现,石子数很少,

    只有100,那么石子与石子之间就会有很大的空白,那么我们可以将这段空白等效于一段很小的空白,实际上由于跳的范围是1-10,那么最多,可以用10*(10-1)来代替

    至于为什么,我不想证啦(如果实在想知道,可以问我,我可以为你用一种神奇的方法证一遍)。

    当然如果最小步数和最大步数相同,那么就不能用这种方法,但是只要石子的位置是步数的倍数,就一定会踩到。

    然后,就没有然后呢。

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <fstream>
     4 #include <cstdlib>
     5 #include <cstring>
     6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     7 using namespace std;
     8 //ifstream fin("新建文本文档.txt");
     9 
    10 int cnt_len=0,cnt_ston=0;
    11 int cd_min=0,cd_max=0;
    12 int pos_ston[105];
    13 bool hash_ston[1000005];
    14 int jiyi[1000005];
    15 bool ssort(int a,int b);
    16 int dg(int now);
    17 
    18 bool ssort(int a,int b){
    19 if(a<b)return 1;
    20 return 0;    
    21 }
    22 
    23 
    24 int dg(int now){
    25 if(now>=cnt_len)return 0;    
    26 if(jiyi[now]!=1061109567)return jiyi[now];    
    27 int jia=0;
    28 if(hash_ston[now]==1)jia=1;
    29 for(int x=cd_min;x<=cd_max;x++)jiyi[now]=min(jiyi[now],dg(now+x));    
    30 jiyi[now]=jiyi[now]+jia;
    31 return jiyi[now];    
    32 }
    33 
    34 
    35 int main(int argc, char** argv) {
    36 cin>>cnt_len>>cd_min>>cd_max>>cnt_ston;
    37 for(int x=1;x<=cnt_ston;x++)cin>>pos_ston[x];
    38 int ans=0;
    39 if(cd_min==cd_max){
    40  for(int x=1;x<=cnt_ston;x++)if(pos_ston[x]%cd_min==0)ans++;
    41  cout<<ans;return 0;    
    42 }
    43 sort(pos_ston+1,pos_ston+1+cnt_ston,ssort);
    44 int a=0;
    45 for(int x=1;x<=cnt_ston;x++){
    46  int b=pos_ston[x]-pos_ston[x-1];    
    47  if(b>90)b=90;
    48  a+=b;
    49  hash_ston[a]=1;
    50 }
    51 cnt_len=a+1;
    52 memset(jiyi,127/2,sizeof(jiyi));
    53 //cout<<jiyi[1]<<endl;
    54 ans=dg(0);
    55 cout<<ans;
    56 return 0;
    57 }

  • 相关阅读:
    awk 入门教程【转】
    xargs 命令教程【转】
    redis 执行lua脚本
    SpringMVC 执行过程分析
    EurekaClient 服务注册、发现、续约
    Springboot 自动配置 & 自定义Starter
    Netty 实现HttpServer
    Spring中@Import 用法
    Netty自定义任务&Future-Listener机制
    Springboot + Netty + WebSocket 实现简单的聊天
  • 原文地址:https://www.cnblogs.com/Ateisti/p/5631179.html
Copyright © 2011-2022 走看看