首页 > 其他分享 >C. Lexicographically Largest

C. Lexicographically Largest

时间:2024-06-17 16:10:28浏览次数:9  
标签:code int 题解 cin Largest Lexicographically

原题链接

题解

1.第 \(i\) 个位置上的数,对 \(S\) 的贡献最大不会超过 \(a_i+i\),我们令其为 \(c_i\)
2.我们不去细想如何操作才能得到最优解,而是去想第一大的 \(b\) 是多少?第二大的 \(b\) 是多少?
3.对 \(c_i\) 降序排序得到 \(b_i\),如果所有 \(b_i\) 都不同,那么直接输出即可。
4.但如果存在 \(k\) 个 \(b_i\) 相同,那么我们先将这 \(k\) 个数标为 \(b_j,b_{j+1},b_{j+2}...b_{j+k-1}\) ,易得 \([1,j]\) 的 \(b\) 不变,而从 \(b_{j+1}\) 开始,每个数最大可以为前面一个数减一

code

#include<bits/stdc++.h>
using namespace std;
int a[300006];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            a[i]+=i;
        }

        sort(a+1,a+1+n,greater<int>());

        for(int i=2;i<=n;i++) a[i]=min(a[i],a[i-1]-1);
        for(int i=1;i<=n;i++) cout<<a[i]<<" \n"[i==n];
    }
    return 0;
}

标签:code,int,题解,cin,Largest,Lexicographically
From: https://www.cnblogs.com/pure4knowledge/p/18252628

相关文章

  • [LeetCode] 2789. Largest Element in an Array after Merge Operations
    Youaregivena0-indexedarraynumsconsistingofpositiveintegers.Youcandothefollowingoperationonthearrayanynumberoftimes:Chooseanintegerisuchthat0<=i<nums.length-1andnums[i]<=nums[i+1].Replacetheelementnums......
  • AT_abc343_f [ABC343F] Second Largest Query 题解
    分析考虑乱搞。对于求次大值,用线段树维护就行了。记录下每个区间的最大、次大值。则两个子区间的父区间的最大值就是这四个最大的,次大值就是这四个次大的。复杂度\(O(\logn)\)。求次大值的出现次数,乱搞就行了。因为带修,带修莫队或者分块有些麻烦。其实用线段树就行。在维护区......
  • ULID(Universally Unique Lexicographically Sortable Identifier)是一种用于生成全局唯
    ULID(UniversallyUniqueLexicographicallySortableIdentifier)是一种用于生成全局唯一、可按字典序排序的标识符的格式。ULID结合了时间戳和随机数的特性,旨在提供高性能、低碰撞、可排序和易读的标识符。ULID的主要特点包括:全局唯一性:通过结合时间戳和随机数的方式,ULID可以生......
  • [LeetCode] 2583. Kth Largest Sum in a Binary Tree
    Youaregiventherootofabinarytreeandapositiveintegerk.Thelevelsuminthetreeisthesumofthevaluesofthenodesthatareonthesamelevel.Returnthekthlargestlevelsuminthetree(notnecessarilydistinct).Iftherearefewerthan......
  • [LeetCode] 1363. Largest Multiple of Three 形成三的最大倍数
    Givenanarrayofdigits digits,return thelargestmultipleof three thatcanbeformedbyconcatenatingsomeofthegivendigitsin anyorder.Ifthereisnoanswerreturnanemptystring.Sincetheanswermaynotfitinanintegerdatatype,returnt......
  • Largest Subsequence
    操作:选取词性最大的子序列,向右循环一次问你进行多少次这样的操作能使数组有序,如果不能就输出-1思路:首先要知道的是一个词性最大的序列整个右移过后,数组的新词性最大的序列就是之前的词性最大序列去了最后一个字母.找出词性最大的子序列intn; strings......
  • Make Lexicographically Smallest Array by Swapping Elements
    MakeLexicographicallySmallestArraybySwappingElementsYouaregivena 0-indexed arrayof positive integers nums anda positive integer limit.Inoneoperation,youcanchooseanytwoindices i and j andswap nums[i] and nums[j] if |nums......
  • 浏览器关于 Largest Contentful Paint (LCP) 的计算机制
    LargestContentfulPaint(LCP)是一种用户体验的性能指标,旨在帮助开发者了解用户在浏览网页时视觉渲染的速度。LCP主要衡量的是视觉上最大的页面元素何时出现在屏幕上,这包括图像元素、视频元素或者包含文本的元素(如段落或列表项)。如果LCP时间较长,用户可能会感觉到页面加载速......
  • SPOJ1805 HISTOGRA - Largest Rectangle in a Histogram 题解
    LinkSPOJ1805HISTOGRA-LargestRectangleinaHistogramQuestion在一条水平线上有\(n\)个高为\(a_i\)的矩形,求包含于这些矩形的最大子矩形面积。Solution我们定义\(L_i\)表示有\(a_i\)这个高度的一根悬线,往左最多能平移到什么位置初始化显然,\(a_i=i\)考虑转移......
  • 什么是前端应用开发的 LCP(Largest Contentful Paint) 指标
    在网页性能优化的领域里,LCP(LargestContentfulPaint,最大内容绘制)是一个非常重要的性能指标。它测量的是从页面开始加载到页面的"主要内容"完全呈现在屏幕上所需的时间。换句话说,LCP是测量用户何时看到页面的"主要内容"的指标。在理解LCP之前,我们需要知道一个概念,那就是......