题目大意

给出一个歌单(有n首歌),每个歌都有愉悦值和时间,你可以选择从第x首歌开始听(也就是选择连续的一段),并且你可以选择w首歌让它的时间减半,限制时间为k,求最大的愉悦值

首先我们需要贪心一下,假如从第x首歌开始听,那么要想获得最大的愉悦值,就必须把那些时间最长的歌进行减半处理。

根据这个,我们就需要利用数据结构来进行维护

考虑使用两个set来维护,S1中保存没有被减半的歌曲,S2中保存减半了的歌曲

首先从x=1开始听,每新加进来一首歌i,进行如下处理

1、S2中还没有w首歌,就直接放进S2里

2、S2中已经有了w首歌,那么就抽出其中时间最短的歌与i比较,如果i的时间大,就把那个最短的歌放到S1中,把i放到S2中;否则就把i放到S1中

假如无法放入歌曲,那么就统计出来当前的愉悦值,然后把第一首歌删掉

删除时要注意,如果删掉的元素在S1中就不需要额外处理,如果在S2中,就要把S1中最长的歌再放到S2中

然后直到最后一首歌被抽出,输出最大的答案

每首歌只会被放入或抽出1次,所以复杂度是nlogn

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
const int maxn = *;
struct Data
{
int t, id;
Data() {}
Data(int _t, int _id) { t = _t; id = _id; }
bool operator <(const Data& B) const
{ return (t == B.t) ? (id < B.id) : t < B.t; }
bool operator == (const Data& B) const
{ return t == B.t && id == B.id; }
};
struct node
{
int t, v;
}a[maxn];
set<Data> S1, S2;
int nowt = , tot = -, st = ;
void Insert(int ty, int t, int id)
{
if(ty == )
{
S1.insert(Data(t, id));
nowt += t;
} else
{
S2.insert(Data(t, id));
nowt += ((t-)/+);
}
} void Erase(int ty, int t, int id)
{
if(ty == )
{
S1.erase(Data(t, id));
nowt -= t;
} else
{
S2.erase(Data(t, id));
nowt -= ((t-)/+);
}
} void ERASE(int st)
{
if(S1.find(Data(a[st].t, st)) != S1.end()) Erase(, a[st].t, st);
else
{
Erase(, a[st].t, st);
if(S1.size() > )
{
Data tmp = *(--S1.end());
Erase(, tmp.t, tmp.id);
Insert(, tmp.t, tmp.id);
}
}
} int n, w, k;
int main()
{
cin>>n>>w>>k;
for(int i = ; i < n; i++) cin>>a[i].v;
for(int i = ; i < n; i++) cin>>a[i].t;
int ans = , ANS = ;
while(st != n)
{
while(nowt <= k)
{
tot++;
if(tot >= n) break;
if(S2.size() < w)
{
Insert(, a[tot].t, tot);
}
else
{
Data tmp = *S2.begin();
if(tmp.t < a[tot].t)
{
Erase(, tmp.t, tmp.id);
Insert(, tmp.t, tmp.id);
Insert(, a[tot].t, tot);
} else
Insert(, a[tot].t, tot);
}
if(nowt <= k) ans += a[tot].v;
else
{
ERASE(tot);
tot--;
break;
}
ANS = max(ANS, ans);
}
ERASE(st);
ans -= a[st].v;
st++;
}
cout<<ANS<<endl;
}

Codeforces Round #386 (Div. 2) 746F(set的运用)的更多相关文章

  1. Codeforces Round #386 (Div. 2)

    迟到的一次比赛 最近状态很崩溃 网速很慢 然后前面五题看了都有打 但是 只有A B E 是过了的 是时候要反省一下 A.随便判断一下就好 最少的份数嘛 B.画出来之后是一下子左边一下子右边 打一个递归 ...

  2. Codeforces Round #386 (Div. 2) C. Tram

    C. Tram time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  3. Codeforces Round #386 (Div. 2) C D E G

    一场比较简单的题 比较脑洞 C 如果坐车比较快的话 先走不如等车 所以最后的ans是min(纯走路,纯坐车) 讨论一下坐车时间 D 因为k一定是>=1的 所以当a=b的时候 GBGBGB这样间隔 ...

  4. Codeforces Round #386 (Div. 2) 746G(树的构造)

    大体题意 一棵树有n个结点,告诉你每层深度上有a[i]个结点,以及有多少叶子结点 让你生成这棵树 题解:考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点 那么答案很简单,就是对(a[ ...

  5. Codeforces Round #386 (Div. 2) A+B+C+D!

    A. Compote 水题(数据范围小都是水题),按照比例找最小的就行了,3min水过. int main() { int a,b,c; while(~scanf("%d%d%d" ...

  6. Codeforces Round #386 (Div. 2)G. New Roads [构造][树]

    题目链接:G. New Roads 题意:给出n个结点,t层深度,每层有a[i]个结点,总共有k个叶子结点,构造一棵树. 分析: 考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点 那 ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. pod 指令无效

    到了新公司,配置pod,死活找不到pod指令,用了很多方法之后,找到了解决办法 sudo vim .bash_profile 然后添加 export PATH=/usr/local/bin:$PATH ...

  2. 10^9以上素数判定,Miller_Rabin算法

    #include<iostream> #include<cstdio> #include<ctime> #include<string.h> #incl ...

  3. docker swarm使用keepalived+haproxy搭建基于percona-xtradb-cluster方案的高可用mysql集群

    一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...

  4. 懒下载软件,一行代码连接wifi^_^

    按键盘的windows+R,输入cmd,回车键 设置语句netsh wlan set hostednetwork mode=allow ssid=user key=possword 按回车键 启动语句 ...

  5. 屏蔽datatable错误提示

    $.fn.dataTable.ext.errMode = 'none'; //不显示任何错误信息// 以下为发生错误时的事件处理,如不处理,可不管.$('#productionRequestItems ...

  6. Linux apt & yum 及 常用命令

    yum yum 语法 yum [options] [command] [package ...] options:可选,选项包括-h(帮助),-y(当安装过程提示选择全部为"yes" ...

  7. 柱状图多系列php动态实现(ec)

    <?php require_once 'data.php'; $arr1=$a->sum('answer','ask_id=1'); $arr2=$a->sum('answer',' ...

  8. MyFirstDay(附6篇python亲历面试题)

    一直以来都是在看别人写的内容,学习前辈们的经验,总感觉自己好像没有什么值得拿出来分享和交流的知识,最近在准备换工作(python后端开发),坐标上海,2019年3月,半个月面了6家(感觉效率是真不高. ...

  9. POJ:3259-Wormholes(最短路判断负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58153 Accepted: 21747 Descripti ...

  10. 笔记-爬虫-模拟登录github

    笔记-模拟登录github 1.      模拟登录github 1.1.    环境准备 安装/升级requests 2.20.0 pip install --upgrade requests pi ...