C. Playlist(优先队列+排序)

本文解析了CodeForces竞赛中一道关于歌曲选择的问题。通过排序和优先队列实现算法,寻找k首歌曲使得总价值最大。代码示例清晰展示了算法步骤。

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

题目链接:http://codeforces.com/contest/1140/problem/C

题目大意:给你一个n首歌的t和b,你最多从其中选择k首,使得这k首歌的t值之和乘以这k首歌最小的b值得到的结果最大。

思路:首先你可以对这n首哥的b排序,然后选择大于等于b的最大的t值之和。从后往前扫一遍,sum维护和,用优先队列存储一下当前选择的t值。

代码入下:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 3e5+7;
const int maxm = 1e6+7;
const int inf = 0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> P;

P a[maxn];
struct node{
	int l;
	int r;
	bool operator<(const node &x)const{
		return l < x.l;
	}
};

int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	ll ans = 0;
	for(int i = 0;i < n; i++){	
		scanf("%d%d",&a[i].second,&a[i].first);
	}
	sort(a,a+n);
	ll sum = 0,res = 0;
	priority_queue<int,vector<int>,greater<int> > que;
	for(int i = n-1;i >= 0; i--){
		que.push(a[i].second);
		sum += a[i].second;
		if(que.size() > k){
			sum -= que.top();
			que.pop();
		}
		res = sum*a[i].first;
		ans = max(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}

 

onReady() { this.audio = wx.getBackgroundAudioManager(); this.setData({ current: this.data.playlist[0], playing: false }); }, setTrack(index) { const track = this.data.playlist[index]; this.audio.title = track.title; this.audio.singer = track.singer; this.audio.coverImgUrl = track.cover; this.audio.src = track.src; // ❌ 不自动播放!以下这行注释掉(或删掉) // this.audio.play(); this.setData({ currentIndex: index, current: track }); }, togglePlay() { if (this.data.playing) { this.audio.pause(); } else { // 如果没有设置 src(首次点击),则先设置音频数据 if (!this.audio.src || this.audio.src !== this.data.current.src) { this.audio.title = this.data.current.title; this.audio.singer = this.data.current.singer; this.audio.coverImgUrl = this.data.current.cover; this.audio.src = this.data.current.src; } this.audio.play(); } this.setData({ playing: !this.data.playing }); }, prevTrack() { const newIndex = (this.data.currentIndex - 1 + this.data.playlist.length) % this.data.playlist.length; this.setData({ playing: true }); this.setTrack(newIndex); }, nextTrack() { const newIndex = (this.data.currentIndex + 1) % this.data.playlist.length; this.setData({ playing: true }); this.setTrack(newIndex); }, rewind() { this.audio.seek(Math.max(this.audio.currentTime - 10, 0)); }, forward() { this.audio.seek(this.audio.currentTime + 10); }, setupProgress() { setInterval(() => { this.setData({ progress: this.audio.currentTime / this.audio.duration * 100 || 0 }); }, 1000); }, onSeeking(e) { const ratio = e.detail.value / 100; const time = ratio * this.audio.duration; this.audio.seek(time); },小程序音乐播放相关代码如下,应该如何修改
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值