传送门

写出来\(n^2\)就有81pts……

\(n^2\)的话枚举最后成为限制的是哪两个矩形,利用前缀和和二分\(n^3\)化\(n^2\)就行了

这题最无脑直接贪心的方法会有后效性

但实际上正解好像就是处理这类问题的一大套路

在两个维度上贪心会有后效性,那选一维枚举,另一维贪心就好了

想了很久也没有想出如何维护\(b_{min}\),题解里说用堆,但用堆我只会\(O(nmlogn)\)复杂度的做法丢人,我暴力都\(O(n^2)\)

思路一直卡在在枚举删\(a_{min}\)的个数时,涉及到在按\(b\)排序的堆中删除\(a\)最小的元素

后来发现不用那么复杂,可以倒序枚举个数,每次加进\(a\)最大的元素

而且堆也没必要每次都清空

发现倒序枚举的实际意义相当于少删一个\(a_{min}\),多删一个\(b_{min}\)

所以维护的时候直接删掉堆里\(b\)最小的,放进去堆外面\(a\)最大的即可

Code:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
#define ld long double
#define usd unsigned
#define ull unsigned long long
//#define int long long #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
char buf[1<<21], *p1=buf, *p2=buf;
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
} int n, m;
ll a[N], b[N]; namespace force{
void solve() {
ll ans=0, amin=INF, bmin=INF;
int lim=1<<n;
for (int s=0,s2,cnt; s<lim; ++s) {
s2=s; cnt=0; amin=INF, bmin=INF;
while (s2) {++cnt; s2&=s2-1;}
if (cnt>m) continue;
for (int i=0; i<n; ++i) {
if (!(s&(1<<i))) {
amin=min(amin, a[i+1]);
bmin=min(bmin, b[i+1]);
}
}
ans=max(ans, amin*bmin);
}
printf("%lld\n", ans);
}
} namespace task1{
void solve() {
sort(b+1, b+n+1);
printf("%lld\n", a[1]*b[m+1]);
}
} namespace task2{
void solve() {
ll amin=INF, bmin=INF;
for (int i=1; i<=n; ++i) amin=min(amin, a[i]), bmin=min(bmin, b[i]);
printf("%lld\n", amin*bmin);
}
} namespace task3{
void solve() {
ll ans=0;
for (int i=1; i<=n; ++i) ans=max(ans, a[i]*b[i]);
printf("%lld\n", ans);
}
} namespace task4{
struct rat{ll a, b; inline void build(ll a_, ll b_) {a=a_; b=b_;}}p[N];
inline bool operator < (rat a, rat b) {return a.a==b.a?a.b<b.b:a.a<b.a;}
ll b2[N];
void solve() {
int tot, dlt; ll ans=0;
for (int i=1; i<=n; ++i) p[i].build(a[i], b[i]);
sort(p+1, p+n+1);
for (int i=1; i<=min(n, m+1); ++i) {
//cout<<"i: "<<i<<endl;
tot=0; dlt=i-1;
for (int j=i+1; j<=n; ++j) if (p[j].b<p[i].b) b2[++tot]=p[j].b;
sort(b2+1, b2+tot+1);
if (dlt+tot<=m) ans=max(ans, p[i].a*p[i].b);
//cout<<"tot: "<<tot<<' '<<dlt<<endl;
//cout<<"b2: "; for (int j=1; j<=tot; ++j) cout<<b2[j]<<' '; cout<<endl;
for (int j=i+1,t; j<=n; ++j) {
t = lower_bound(b2+1, b2+tot+1, p[j].b)-b2-1;
//cout<<"t: "<<t<<endl;
if (dlt+t>m) continue;
else ans=max(ans, p[i].a*min(p[i].b, p[j].b));
//cout<<"upd: "<<i<<' '<<j<<' '<<p[i].a*min(p[i].b, p[j].b)<<endl;
}
//cout<<"ans: "<<ans<<endl<<endl;
}
printf("%lld\n", ans);
}
} namespace task5{
struct rat{ll a, b; inline void build(ll a_, ll b_) {a=a_; b=b_;}}p[N];
inline bool operator < (rat a, rat b) {return a.a==b.a?a.b<b.b:a.a<b.a;}
struct ele{ll a, b; ele(ll a_, ll b_):a(a_),b(b_){} ele(){}};
inline bool operator < (ele a, ele b) {return a.b>b.b;}
priority_queue<ele> q;
void solve() {
//if (n==1) {printf("%lld\n", a[1]*b[1]); return ;}
for (int i=1; i<=n; ++i) p[i].build(a[i], b[i]);
//for (int i=1; i<=n; ++i) cout<<p[i].a<<','<<p[i].b<<endl;
sort(p+1, p+n+1);
ll ans=0, amin=INF, bmin=INF;
for (int i=0; i<=m; ++i) {
//cout<<"i: "<<i<<endl;
amin=INF, bmin=INF;
for (int j=i+1; j<=n; ++j) q.push(ele(p[j].a, p[j].b));
for (int j=i+1; j<=m; ++j) q.pop();
while (!q.empty()) {amin=min(amin, q.top().a); bmin=min(bmin, q.top().b); q.pop();}
//cout<<"min: "<<amin<<' '<<bmin<<endl;
ans=max(ans, amin*bmin);
}
printf("%lld\n", ans);
}
} namespace task{
struct rat{ll a, b; inline void build(ll a_, ll b_) {a=a_; b=b_;}}p[N];
inline bool operator < (rat a, rat b) {return a.a==b.a?a.b<b.b:a.a<b.a;}
priority_queue< int, vector<int>, greater<int> > q;
void solve() {
//if (n==1) {printf("%lld\n", a[1]*b[1]); return ;}
for (int i=1; i<=n; ++i) p[i].build(a[i], b[i]);
//for (int i=1; i<=n; ++i) cout<<p[i].a<<','<<p[i].b<<endl;
sort(p+1, p+n+1);
ll ans=0;
while (!q.empty()) q.pop();
for (int i=m+1; i<=n; ++i) q.push(p[i].b);
for (int i=m+1; i; --i) {
ans=max(ans, p[i].a*q.top());
q.pop(); q.push(p[i-1].b);
}
printf("%lld\n", ans);
}
} signed main()
{
#ifdef DEBUG
freopen("1.in", "r", stdin);
#endif
int T; T=read();
if (!T) return 0;
while (T--) {
bool equal=1;
n=read(); m=read();
for (int i=1; i<=n; ++i) {
a[i]=read(), b[i]=read();
if (i!=1) {if (a[i]!=a[i-1]) equal=0;}
}
//if (0&&n<=25) force::solve();
//else if (!m) task2::solve();
//else if (equal) task1::solve();
//else if (m==n-1) task3::solve();
//else task4::solve();
task::solve();
} return 0;
}

题解 d的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

  10. JSOI2016R3 瞎BB题解

    题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...

随机推荐

  1. VisibleDeprecationWarning , Creating an ndarray from ragged nested sequences... 警告怎么办

    我不是完美主义,但是至少,我在做实验的时候不能容忍有 warning 的出现. 今天使用 tensorflow.keras.datasets中的 imdb 数据集,使用 imdb.load_data( ...

  2. SpEL表达式总结(转)

    前言 SpEL(Spring Expression Language),即Spring表达式语言,是比JSP的EL更强大的一种表达式语言.为什么要总结SpEL,因为它可以在运行时查询和操作数据,尤其是 ...

  3. Servlet核心技术

    一.基本概念 1.C/S C/S架构是客户端服务器架构,将需要处理的业务合理的分配到客户端和服务器,客户端负责与用户的交互任务,服务器负责数据管理. 优点: 客户端界面和功能可以很丰富 应用服务器负荷 ...

  4. 小哈学Python ----XML

    XML XML是实现不同语言或程序之间进行数据交换的协议,XML文件格式如下: <data> <country name="Liechtenstein"> ...

  5. python encode decode

    Python encode()encode() 方法以 encoding 指定的编码格式编码字符串.errors参数可以指定不同的错误处理方案.写法:str.encode(encoding='UTF- ...

  6. 从0到1认识XHTML

    XHTML概念 XHTML是一种可扩展超文本标记语言,与HTML(超文本标记语言)类似,不过在语法上更加严格.XHTML是以XML(是一种必须正确标记且格式良好的标记语言)应用方式定义的HTML,与h ...

  7. Debian9 无线网卡驱动安装

    https://wiki.debian.org/iwlwifi sudo vi /etc/apt/sources.list --------- ..... deb http://httpredir.d ...

  8. 前端基础html(二)

    一.html的概念 1.概念:超文本标记语言. 2.超文本,超链接:超级不仅有文本,图片,还有音频,视频等. 3.html:作用:   显示服务器端的响应结果. 二.互联网三大基石 1.url:统一资 ...

  9. BOM(Bill of Material)物料清单基础知识(一)

                                                                                            一.BOM的基础概念 概 ...

  10. python批量修改图片名称

    import os class BatchRename(): def rename(self): # windows环境 """ os.rename() 方法用于命名文件 ...