zoukankan      html  css  js  c++  java
  • 17-比赛1 F

    Seg-El has last chance to make the final changes in order to prevent the destruction of Krypton.

    He is provided an array Arr[ ] of N elements.

    For each element Arr [ i ], he needs to find the largest element Arr [ j ] where j < i and Arr [ j ] < Arr [ i ]

    Help him to achieve this task.

    Input

    First line comprises N- the number of elements in the array.

    Second line contains N space separated integers denoting the elements of the array.

    Output

    Print N lines denoting the required answer as specified above. In case no such value exists for some

    Arr [ i ], print "-1" (without quotes).

    Constraints

    • 1 ≤ N ≤ 200000
    • 1 ≤ Arr [ i ] ≤ 1015

    Example


    1 2 3 5 4 

    Output:

    -1 



    通过 set 或 map 就可轻松解决;

    关键字 :  set maplower_bound()函数

    1.学长的代码

     5 # include <bits/stdc++.h>
     6 using namespace std;
     7 set<long long> s;
     8 int main ()
     9 {
    10     int n;
    11     long long x;
    12     scanf("%d", &n);
    13     for (int i = 1; i <= n; ++i) {
    14         scanf("%lld", &x);
    15         x = -x;
    16         auto iter = s.lower_bound(x + 1);
    17         if (iter == s.end()) puts("-1");
    18         else printf("%lld
    ", -*iter);
    19         s.insert(x);
    20     }
    21     return 0;
    22 }

    2 . 一开始不会使用set ,用map自己做的

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main()
     4 {
     5     map<long long,int> imap;
     6     long long t;
     7     int n;
     8     cin>>n;
     9     for(int i =1;i<=n;i++)
    10     {
    11         scanf("%lld",&t);
    12         t = -t;
    13         //map<long long,int>::iterator it;
    14         auto it = imap.lower_bound(t+1);
    15         if(it == imap.end()) cout<<"-1"<<endl;
    16         else
    17            cout<<-(it->first)<<endl;
    18         imap[t] = i;
    19     }
    20     return 0;
    21 }
  • 相关阅读:
    Shell学习笔记 ——第一天
    Myclipse 安装 Maven遇见的N个异常
    Myeclipse 创建 Web Maven项目
    Guava API
    String 转Map(基于Guava类库)
    Mybatis——helloWorld级程序
    redis
    listener、context、filter、servlet及其加载顺序
    junit 单元测试
    hibernate —— 树状存储
  • 原文地址:https://www.cnblogs.com/darkboy/p/9379891.html
Copyright © 2011-2022 走看看