zoukankan      html  css  js  c++  java
  • CH1301 邻值查找【set应用】

    1301 邻值查找 0x10「基本数据结构」例题

    描述

    给定一个长度为 n 的序列 A,A 中的数各不相同。对于 A 中的每一个数 A_i,求:
    min(1≤j<i) ⁡|A_i-A_j|
    以及令上式取到最小值的 j(记为 P_i)。若最小值点不唯一,则选择使 A_j 较小的那个

    输入格式

    第一行一个整数n,第二行n个数A_1~A_n。

    输出格式

    n-1行,每行2个用空格隔开的整数。分别表示当i取2~n时,对应的 min(1≤j<i) ⁡|A_i-A_j| 和 P_i 的值。

    样例输入

    3
    1 5 3

    样例输出

    4 1
    2 1

    数据范围与约定

    • 对于30%的数据: n<=100
    • 对于70%的数据: n<=10^4
    • 对于100%的数据: n<=10^5, |A_i|<=10^9

    思路:

    用set或者是链表实现

    读入第i个时,加入set,set中存的就是i之前的所有数,而且set是默认排好序的。

    然后find找到i的位置,|Aj-Ai|最小的一定是Ai的前驱或者后继。

    判断一下就行了。要注意Ai就是set的头或尾的情况。

    奇怪的是,用printf输出的时候有一点问题不知道是为什么,用cout就行。

     1 // ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
     2 //
     3 
     4 
     5 //#include "pch.h"
     6 #include <iostream>
     7 #include <set>
     8 #include <cmath>
     9 #include <stdio.h>
    10 using namespace std;
    11 
    12 int n;
    13 const int maxn = 1e5 + 5;
    14 struct node {
    15     int a;
    16     int id;
    17     bool operator < (const node &b)const {
    18         return a < b.a;
    19     }
    20 }A[maxn];
    21 set<node>sss;
    22 set<node>::iterator after;
    23 set<node>::iterator before;
    24 
    25 int main()
    26 {
    27     scanf("%d", &n);
    28     scanf("%d", &A[1].a);A[1].id = 1;
    29     sss.clear();
    30     sss.insert(A[1]);
    31     for (int i = 2; i <= n; i++) {
    32         scanf("%d", &A[i].a);
    33         A[i].id = i;
    34         sss.insert(A[i]);
    35         after = sss.find(A[i]);
    36         before = sss.find(A[i]);
    37         //cout<<(*after).id<<endl;
    38         if (after == sss.begin()) {
    39             after++;
    40             cout<<abs((*after).a - A[i].a)<<" "<<(*after).id<<endl;
    41             //printf("%d %d
    ", abs((*after).a - A[i].a), (*after).id);
    42         }
    43         else if (after++, after == sss.end()) {
    44             before--;
    45             //node tmp = *before;
    46             //printf("%d
    ", tmp.a);
    47             cout<<abs((*before).a - A[i].a)<<" "<<(*before).id<<endl;
    48             //printf("%d %d
    ", abs((*before).a - A[i].a), (*before).id);
    49         }
    50         else {
    51             before--;
    52             if (abs((*before).a - A[i].a) < abs((*after).a - A[i].a)) {
    53                 cout<<abs((*before).a - A[i].a)<<" "<<(*before).id<<endl;
    54             }
    55             else if(abs((*before).a - A[i].a) > abs((*after).a - A[i].a)){
    56                 cout<<abs((*after).a - A[i].a)<<" "<<(*after).id<<endl;
    57             }
    58             else {
    59                 if ((*before).a < (*after).a) {
    60                     cout<<abs((*before).a - A[i].a)<<" "<<(*before).id<<endl;
    61                 }
    62                 else {
    63                     cout<<abs((*after).a - A[i].a)<<" "<<(*after).id<<endl;
    64                 }
    65             }
    66         }
    67     }
    68 }
    69 
  • 相关阅读:
    linux C程序中获取shell脚本输出(如获取system命令输出)
    Vue实现网页在线拍照和上传 幸福n
    c# thread数线程的创建多线程(一)
    C#开启异步 线程的四种方式(二)
    web学习网站
    C#中的set和get方法
    C# 多线程之Task任务(三)
    Taro3 扫描不同二维码参数不同,但是热启动之后参数不变
    Taro 弹窗阻止小程序滑动穿透(亲测有效) tabbar数据缓存不更新 入口场景值不同
    《Webpack+Babel入门与实例详解》出版了
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9800787.html
Copyright © 2011-2022 走看看