Codeforces Round 143 (Div. 2) C. To Add or Not to Add 題解 前綴和 二分

To Add or Not to Add

題目描述

A piece of paper contains an array of n n n integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1?,a2?,...,an?. Your task is to find a number that occurs the maximum number of times in this array.

However, before looking for such number, you are allowed to perform not more than k k k following operations — choose an arbitrary element from the array and add 1 1 1 to it. In other words, you are allowed to increase some array element by 1 1 1 no more than k k k times (you are allowed to increase the same element of the array multiple times).

Your task is to find the maximum number of occurrences of some number in the array after performing no more than k k k allowed operations. If there are several such numbers, your task is to find the minimum one.

輸入格式

The first line contains two integers n n n and k k k ( 1 < = n < = 1 0 5 1<=n<=10^{5} 1<=n<=105 ; 0 < = k < = 1 0 9 0<=k<=10^{9} 0<=k<=109 ) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.

The third line contains a sequence of n n n integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1?,a2?,...,an? ( ∣ a i ∣ < = 1 0 9 ) (|a_{i}|<=10^{9}) (ai?<=109) — the initial array. The numbers in the lines are separated by single spaces.

輸出格式

In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k k k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.

提示

In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6 , 4 , 4 , 0 , 4 6,4,4,0,4 6,4,4,0,4, where number 4 4 4 occurs 3 3 3 times.

In the second sample you don’t need to perform a single operation or increase each element by one. If we do nothing, we get array 5 , 5 , 5 5,5,5 5,5,5, if we increase each by one, we get 6 , 6 , 6 6,6,6 6,6,6. In both cases the maximum number of occurrences equals 3 3 3. So we should do nothing, as number 5 5 5 is less than number 6 6 6.

In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3 , 2 , 2 , 2 , 2 3,2,2,2,2 3,2,2,2,2, where number 2 2 2 occurs 4 4 4 times.

題面翻譯

題目描述

給定一個長度為 n n n 的序列 a 1 a1 a1, a 2 a2 a2…… a n an an,請你把其中一些數進行若干次 + 1 +1 +1 操作,且操作總次數不超過 k k k,使得原序列中某數出現的次數最多。求操作之后的出現最多的數和它出現的次數。

輸入

第一行兩個整數 n , k n,k n,k,即序列的長度和操作總次數。第二行為 a 1 a1 a1, a 2 a2 a2…… a n an an

輸出

兩個整數,分別為出現最多的次數和出現最多的。如果有多個滿足條件的數字,輸出值最小的一個。

樣例 #1

樣例輸入 #1

5 3
6 3 4 0 2

樣例輸出 #1

3 4

樣例 #2

樣例輸入 #2

3 4
5 5 5

樣例輸出 #2

3 5

樣例 #3

樣例輸入 #3

5 3
3 1 2 2 1

樣例輸出 #3

4 2

原題

Codeforces——傳送門
洛谷——傳送門

代碼

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
typedef long long ll;int main()
{ios::sync_with_stdio(0);cin.tie(0);int n, k;cin >> n >> k;vector<int> a(n + 1);for (int i = 1; i <= n; i++){cin >> a[i];a[i] += 1e9; // 由于讀入的數據存在負數,我們可以通過巧妙地補償1e9來使數組元素成為非負數}sort(a.begin(), a.end());vector<i64> s(n + 1, 0);for (int i = 1; i <= n; i++) // 前綴和s[i] = s[i - 1] + a[i];auto get_cnt = [&](int idx){// 最大次數即[left,idx]區間的最大長度// 二分leftint l = 1, r = idx;while (l < r){int mid = (l + r) / 2;// 達到idx-mid+1個a[idx]值需要補充1LL * a[idx] * (idx - mid + 1) - (s[idx] - s[mid - 1])的差值// k>=差值,則滿足條件if (k >= 1LL * a[idx] * (idx - mid + 1) - (s[idx] - s[mid - 1]))r = mid;elsel = mid + 1;}return idx - l + 1;};int max_cnt = 0;                   // 某數出現的最大次數int ans_num = -1;                  // 取到最大次數的最小數組元素for (int idx = 1; idx <= n; idx++) // 遍歷數組,確定數組中各個元素可出現的最大次數{int cur_cnt = get_cnt(idx);if (cur_cnt > max_cnt){max_cnt = cur_cnt;ans_num = a[idx];}}cout << max_cnt << ' ' << ans_num - (int)1e9 << '\n'; // 輸出時記得將補償的1e9減去return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/37330.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/37330.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/37330.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

點云壓縮配置開發環境遇到一些bug

1、配置基于cuda的計算庫&#xff0c;Chamfer3D和pointops 編譯chamfer3D時候會遇到一個cub版本的校驗錯誤。 解決方法&#xff1a;根據錯誤提示&#xff0c;進入cuda的config配置文件中&#xff0c;使用#define將校驗功能關閉 編譯pointops&#xff0c;會遇到報錯&#xff1a;…

C++Primer Plus 第十四章代碼重用:14.4.4 數組模板示例和非類型參數2

14.4.4 數組模板示例和非類型參數 提示&#xff1a;這里可以添加系列文章的所有文章的目錄&#xff0c;目錄需要自己手動添加 例如&#xff1a;第一章 Python 機器學習入門之pandas的使用 提示&#xff1a;寫完文章后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右…

《分析模式》漫談08-單繼承不是“唯一繼承”

DDD領域驅動設計批評文集 做強化自測題獲得“軟件方法建模師”稱號 《軟件方法》各章合集 《分析模式》第2章這一段&#xff1a; 劃線處的single inheritance&#xff0c;2004中譯本的翻譯&#xff1a; 翻譯為“單繼承”&#xff0c;是正確的。 2020中譯本的翻譯&#xff1a…

Java NIO(一) 概述

NIO主要用于以少量線程來管理多個網絡連接&#xff0c;處理其上的讀寫等事件。在大量連接情況下&#xff0c;不管是效率還是空間占用都要優于傳統的BIO。 Java NIO 由以下幾個核心部分組成&#xff1a; Channel Buffer Selector Selector 如果你的應用打開了多個連接&#x…

分頁插件 count有數據,代碼不往下執行

如下:如果打印了sql那么當row>0時會有圖2下面sql詳情的輸出 問題出在了分頁參數上,pageNum為1,并且pageSize>2才能打印出圖二的結果,圖一為pageNum值是0,注意,查詢第一頁,分頁應該傳入的是1而不是0

大數據批處理系統和業務系統是兩種不同類型的系統,它們在目的、設計、功能和使用場景上有所區別

大數據批處理系統和業務系統是兩種不同類型的系統&#xff0c;它們在目的、設計、功能和使用場景上有所區別。以下是大數據批處理系統和業務系統之間的一些主要差異&#xff1a; 1. **目的**&#xff1a; - **大數據批處理系統**&#xff1a;主要用于處理和分析大量數據&am…

MySQL高級1.0

目錄 &#x1f4cc;MySQL存儲過程和函數 ??存儲過程和函數介紹 ??存儲過程的創建和調用 ??存儲過程的查看和刪除 ??存儲過程語法-變量 ??存儲過程語法-if語句 ??存儲過程語法-參數傳遞 ??存儲過程語法-while循環 ??存儲過程語法-存儲函數 &#x1f4…

Linux高并發服務器開發(六)線程

文章目錄 1. 前言2 線程相關操作3 線程的創建4 進程數據段共享和回收5 線程分離6 線程退出和取消7 線程屬性&#xff08;了解&#xff09;8 資源競爭9 互斥鎖9.1 同步與互斥9.2 互斥鎖 10 死鎖11 讀寫鎖12 條件變量13 生產者消費者模型14 信號量15 哲學家就餐 1. 前言 進程是C…

【FFmpeg】avio_open2函數

【FFmpeg】avio_open2函數 1.avio_open21.1 創建URLContext&#xff08;ffurl_open_whitelist&#xff09;1.1.1 創建URLContext&#xff08;ffurl_alloc&#xff09;1.1.1.1 查找合適的protocol&#xff08;url_find_protocol&#xff09;1.1.1.2 為查找到的URLProtocol創建UR…

影響Cache命中率的因素有哪些?

緩存命中率&#xff08;Cache Hit Rate&#xff09;是指處理器訪問緩存時&#xff0c;所需數據已經在緩存中找到的次數與總訪問次數的比例。提高緩存命中率可以顯著提升系統性能&#xff0c;因為緩存訪問速度遠快于主存訪問速度。影響緩存命中率的關鍵因素包括&#xff1a; 1.…

C語言異常處理就機制setjmp()和longjmp()

C語言setjmp()和longjmp()實現異常處理機制。 setjmp() 用于保存當前的程序執行狀態。 longjmp() 用于在后面的某個時刻返回到setjmp()點的狀態。 類似goto。但goto是本地的&#xff0c;只能在函數內部跳轉。 setjmp()和longjmp()是非局部跳轉語句&#xff0c;可在調用棧上&a…

通信系統網絡架構_3.移動通信網絡架構

移動通信網為移動互聯網提供了強有力的支持&#xff0c;尤其是5G網絡為個人用戶、垂直行業等提供了多樣化的服務。以下從業務應用角度給出面向5G網絡的組網方式。 1.5GS與DN互連 5GS&#xff08;5G System&#xff09;在為移動終端用戶&#xff08;User Equipment&#xff0c;…

CSRF的其他防范措施?

一般情況下&#xff0c;我們可以通過各種防護策略來防御CSRF&#xff0c;對于QA、SRE、安全負責人等&#xff0c;我們可以做哪些事情來提升安全性呢&#xff1f; 一、CSRF測試 CSRFTester是一款CSRF漏洞的測試工具&#xff0c;CSRFTester工具的測試原理大概是這樣的&#xff…

BLACKBOX.AI:解鎖開發新紀元,加速編程學習的AI神器!

文章目錄 &#x1f4af;BLACKBOX.AI 官網&#x1f341;1 BLACKBOX.AI 工具使用教程&#x1f341;2 BLACKBOX.AI工具使用界面介紹&#x1f341;3 Chat(聊天)功能&#x1f341;4 Explore (探索)功能&#x1f48e;4.1 Terminal(終端)功能&#x1f48e;4.2 Discover(發現)功能&…

STM32 IWDG(獨立看門狗)

1 IWDG簡介 STM32有兩個看門狗&#xff1a;一個是獨立看門狗&#xff08;IWDG&#xff09;&#xff0c;另外一個是窗口看門狗。獨立看門狗也稱寵物狗&#xff0c;窗口看門狗也稱警犬。本文主要分析獨立看門狗的功能和它的應用。 獨立看門狗用通俗一點的話來解釋就是一個12位的…

關于轉BigDecimal對象時,精度問題

//浮點型數值Double d 0.0003d;//轉BigDecimal對象BigDecimal a new BigDecimal(d);System.out.println(String.format("浮點類型數字:%.4f創建BigDecimal對象并且保留多位小數并且保留多位小數時,精度會變多,結果為%s",d,a.setScale(8, BigDecimal.ROUND_DOWN)));…

format()方法——格式化字符串

自學python如何成為大佬(目錄):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 語法介紹 format()可以對數據進行格式化處理操作&#xff0c;語法如下&#xff1a; format(value, format_spec) format_spec為格式化解釋。當參數…

【計算機畢業設計】092基于微信小程序二手閑置交易市場

&#x1f64a;作者簡介&#xff1a;擁有多年開發工作經驗&#xff0c;分享技術代碼幫助學生學習&#xff0c;獨立完成自己的項目或者畢業設計。 代碼可以私聊博主獲取。&#x1f339;贈送計算機畢業設計600個選題excel文件&#xff0c;幫助大學選題。贈送開題報告模板&#xff…

PostgreSQL的系統視圖pg_stat_archiver

PostgreSQL的系統視圖pg_stat_archiver 在 PostgreSQL 數據庫中&#xff0c;pg_stat_archiver 視圖提供了關于歸檔進程&#xff08;archiver process&#xff09;的統計信息。歸檔進程負責將 WAL&#xff08;Write-Ahead Logging&#xff09;日志文件復制到歸檔存儲&#xff0…

探索區塊鏈:顛覆性技術的崛起

目錄 一、引言 二、區塊鏈技術概述 三、區塊鏈應用場景 四、區塊鏈面臨的挑戰 五、區塊鏈的未來展望 六、結語 一、引言 在數字化浪潮的推動下&#xff0c;區塊鏈技術以其獨特的去中心化、透明性和不可篡改性等特性&#xff0c;正在逐步改變我們的生活。從金融領域到供應…