zoukankan      html  css  js  c++  java
  • codeforces 558B. Amr and The Large Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/558/B

    题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值。

      由于是边读入边比较,因此问题最关键的是,记录每个数第一次出现的位置,即左值。因为要保证次数是出现最多,因此需要一个cnt[]数组来记录出现次数。然后当最多出现次数与当前cnt[x]次数相同时,要选择区间较短的,再更新左右区间值。

      赛中短路竟然想不出来~~~泪啊~~泪啊- >_<

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int maxn = 1e6 + 5;
     9 int cnt[maxn];
    10 int pl[maxn];   // 记录每个数第一次出现的位置
    11 
    12 int main()
    13 {
    14     #ifndef ONLINE_JUDGE
    15         freopen("in.txt", "r", stdin);
    16     #endif // ONLINE_JUDGE
    17 
    18     int n, a;
    19     while (scanf("%d", &n) != EOF) {
    20         memset(cnt, 0, sizeof(cnt));
    21         memset(pl, 0, sizeof(pl));
    22         int max_cnt = 0;
    23         int min_dis = 0;
    24         int l = 0, r = 0;
    25 
    26         for (int i = 0; i < n; i++) {
    27             scanf("%d", &a);
    28 
    29             if (!cnt[a]) {
    30                 pl[a] = i;
    31             }
    32             cnt[a]++;
    33 
    34             if (max_cnt < cnt[a]) {
    35                 max_cnt = cnt[a];
    36                 l = pl[a], r = i;
    37                 min_dis = i - tl;
    38             }
    39             if (max_cnt == cnt[a] && i-pl[a] < min_dis) {
    40                 min_dis = i - pl[a];
    41                 l = pl[a];
    42                 r = i;
    43             }
    44         }
    45         printf("%d %d
    ", l+1, r+1);
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    EF中嵌套类的where查询
    VS中添加Web References
    DropDownList绑定数据源后,要插入项的处理
    CheckBoxList选中某项,获取其它项是否是选中
    WebAPI的使用
    HTML5中像网页中保存cookie的实现
    日志切割之Logrotate
    CentOS防火墙iptables使用
    CentOS7安装Python3
    Keepalived高可用
  • 原文地址:https://www.cnblogs.com/windysai/p/4652600.html
Copyright © 2011-2022 走看看