• USACO Breed Proximity


    Breed Proximity

    TimeLimit: 2 Second   MemoryLimit: 64 Megabyte

    Totalsubmit: 46   Accepted: 10  

    Description

    Farmer John's N cows (1 <= N <= 50,000) are standing in a line, each
    described by an integer breed ID.

    Cows of the same breed are at risk for getting into an argument with
    each-other if they are standing too close.  Specifically, two cows of the
    same breed are said to be "crowded" if their positions within the line
    differ by no more than K (1 <= K < N).  

    Please compute the maximum breed ID of a pair of crowded cows.

    Input

    * Line 1: Two space-separated integers: N and K.

    * Lines 2..1+N: Each line contains the breed ID of a single cow in the
           line.  All breed IDs are integers in the range 0..1,000,000.
    There are 6 cows standing in a line, with breed IDs 7, 3, 4, 2, 3, and 4. 
    Two cows of equal breed IDs are considered crowded if their positions
    differ by at most 3.

    Output

    * Line 1: The maximum breed ID of a crowded pair of cows, or -1 if
           there is no crowded pair of cows.
    The pair of cows with breed ID 3 is crowded, as is the pair of cows with
    breed ID 4

    Sample Input

    6 3
    7
    3
    4
    2
    3
    4

    Sample Output

    4

    Source

    USACO

     
    思路:
     
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    int map[51010];
    int can_be[1100000];
    int the_last_max;
    int n,k;
    int main()
    {
        while(~scanf("%d%d",&n,&k))
        {
            memset(map,0,sizeof(map));
            memset(can_be,0,sizeof(can_be));
            int the_k;
            the_last_max = -1;
            for(int i = 1;i <= n;i ++)
            {
                scanf("%d",&map[i]);
                if(i - k - 1 >= 1)
                      can_be[map[i - k - 1]] = 0;
                if(can_be[map[i]] == 1 && map[i] > the_last_max)
                     the_last_max = map[i];
                can_be[map[i]] = 1;
            }
            printf("%d
    ",the_last_max);
        }
        return 0;
    }
  • 相关阅读:
    json数据转为对象,一般在前台把数据传回后端中使用 转https://www.cnblogs.com/zxtceq/p/6610214.html
    JavaScript事件大全3
    javascript大全
    Javascript刷新页面大全
    JavaScript验证正则表达式大全
    insertAdjacentHTML和insertAdjacentText方法
    CSS实现样式布局
    iis浏览网页时提示无法显示 XML 页
    IIS浏览提示无法显示网页的解决方法
    在dreamweaver中输入代码时不会有提示的解决办法
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3426706.html
Copyright © 2020-2023  润新知