D. Cutting Out

本文详细解析了Codeforces竞赛中一道经典题目,采用二分贪心算法解决子串数量最大化的难题,通过统计数字频率、排序及逆序,最终实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

---恢复内容开始---

链接

[https://codeforces.com/contest/1077/problem/D]

题意

给你n,k,n个数,找出长度为k,的子串(不需连续),使得该子串数量最多

分析

1.肯定统计每个数字的数量
2.看那些数字数量大于0,保存数量和该数数值
3.对保存的根据数量从小到大排序
4.reverse就变成从大到小排序
5.选出前数量前k大的前k个数,
6.二分贪心查找
很经典的二分吧

代码

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N=2e5+10;
int f[N];
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 
    int n,k,x,i,j,ma; 
    ma=0;
    //freopen("in.txt","r",stdin);
    cin>>n>>k;
    for(i=0;i<n;i++){
        cin>>x; f[x]++; ma=max(ma,x);
    }
    vector<pair<int,int> > v1,v2;
    for(i=1;i<=ma;i++) if(f[i]) v1.pb(mp(f[i],i));
    sort(v1.begin(),v1.end());
    reverse(v1.begin(),v1.end());
    j=0;
    for(i=0;i<v1.size();i++)
    {
        j++; if(j>k) break;
        v2.pb(mp(v1[i].fi,v1[i].se));
    }
    
    vector<int> ans;
    int l=1,r=N;
    while(l<=r){
        int mid=(l+r)>>1;
        int sum=0;
        vector<int> tem;
        for(i=0;i<v2.size();i++)
        tem.pb(v2[i].fi/mid),sum+=tem[i];
        if(sum>=k){
            ans.clear();
            int cnt=0;
            for(i=0;i<v2.size();i++){
                int t=tem[i];
                while(cnt<k&&t--)
                ans.pb(v2[i].se),cnt++;
            }
            l=mid+1;
        }
        else r=mid-1;
    }
    for(i=0;i<ans.size();i++)
    cout<<ans[i]<<' ';
    cout<<endl;
    return 0;
} 

转载于:https://www.cnblogs.com/mch5201314/p/10068303.html

TCUTTER - Tin Cutter In a Tin Cutting factory there is a machine for cutting parts from tin plates. It has an extraordinarily sharp knife able to make horizontal or vertical segment cuts in the tin plates. Each cutting process consists of a sequence of such cuts. Each segment cut is given by its endpoints that are always located inside the tin plate. During the cutting process some parts of tin plate can fall out and so some holes in the plate can emerge. Factory management needs to predict the number of holes in the plate at the end of the given sequence of cuts. Write a program that answers this question. Single segment cuts are not considered to be holes. Here there are examples of some situations that can arise after cutting: two holes two holes one hole one hole Input The input file consists of blocks of lines. Each block except the last describes one cutting process. In the first line of the block there is a number N <= 100. indicating the number of segment cuts in the cutting process. These cuts are defined by the following N lines. The line defining one segment cut has the form X1 Y1 X2 Y2 where X1 Y1 and X2 Y2 are the co-ordinates of the end points of the segment cut. They are separated by one space. The co-ordinates are integers and always define horizontal or vertical segment (i.e. segment parallel with x or y axis). The last block consists of just one line containing 0. Output The output file contains the lines corresponding to the blocks in the input file. Each such line contains the number of holes that remain in the tin plate after the execution of the corresponding cuts. There is no line in the output file corresponding to the last "null" block of the input file. Example Input: 4 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 2 0 1 2 1 1 2 1 0 0 Output: 1 0
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值