zoukankan      html  css  js  c++  java
  • Codeforces Round #602 Div2 D1. Optimal Subsequences (Easy Version)

    题意:给你一个数组a,询问m次,每次返回长度为k的和最大的子序列(要求字典序最小)的pos位置上的数字.

    题解:和最大的子序列很简单,排个序就行,但是题目要求字典序最小,那我们在刚开始的时候先记录每个数的位置再排序,然后选出k个最大的数后在对位置从小到大排个序就行了(这题有个坑,第一次排序的时候记得把相等的数按位置小的排在前面).

    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <unordered_set>
    12 #include <unordered_map>
    13 #define ll long long
    14 #define fi first
    15 #define se second
    16 #define pb push_back
    17 #define me memset
    18 const int N = 1e6 + 10;
    19 const int mod = 1e9 + 7;
    20 using namespace std;
    21 typedef pair<int,int> PII;
    22 typedef pair<long,long> PLL;
    23  
    24 int n,m;
    25 int k,pos;
    26 PII a[N];
    27 vector<PII> tmp;
    28  
    29 bool cmp1(PII a,PII b){
    30     if(a.fi==b.fi) return a.se<b.se;
    31     return a.fi>b.fi;
    32 }
    33  
    34 bool cmp2(PII a,PII b){
    35     return a.se<b.se;
    36 }
    37  
    38 int main() {
    39     ios::sync_with_stdio(false);
    40     cin>>n;
    41      for(int i=0;i<n;++i){
    42          cin>>a[i].fi;
    43         a[i].se=i;
    44      }
    45      sort(a,a+n,cmp1);
    46     cin>>m;
    47      while(m--){
    48          cin>>k>>pos;
    49          tmp.clear();
    50          for(int i=0;i<k;++i) tmp.pb(a[i]);
    51         sort(tmp.begin(),tmp.end(),cmp2);
    52         printf("%d\n",tmp[pos-1].fi);
    53      }
    54  
    55     return 0;
    56 }
    View Code

    总结:学会使用pair!!

  • 相关阅读:
    properties文件不能输入中文
    java: bin里面的.class文件没有了怎么办
    LINUX 系统java自动化启动浏览器 提示:The driver is not executable: /home/pt/Downloads/googledriver/chromedriver_linux64/chromedriver
    MarkdownTest
    洛谷P5364 [SNOI2017]礼物 题解
    长链剖分
    左偏树(可并堆)
    Splay
    分层图最短路
    整体二分
  • 原文地址:https://www.cnblogs.com/lr599909928/p/12778715.html
Copyright © 2011-2022 走看看