[SimpleOJ239]Cards
题目大意:
有n(n为偶数)张牌,每张牌正反面有两张数字,你可以从中选出n/2张牌,减去某一面的数字,再选出另外n/2张牌,加上某一面的数字,问最终的答案最小能是多少?
思路:
先不考虑n/2的限制,考虑每张牌的最优情况——加上较小值,减去较大值。
对每张牌记录一下两种情况的差值,以及加的个数和减的个数。
看一下是不是刚好n/2,如果不是,就加上那些差值。
注意两种情况的差值是要分开记录的,一开始没想清楚,只用了一个堆来维护,只能拿50分。
#include<stack>
#include<vector>
#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int B=;
struct Point {
int x,y;
bool operator < (const Point &another) const {
if(y==another.y) return x>another.x;
return y<another.y;
}
};
std::vector<Point> a,p;
std::stack<int> q;
int l[B],r[B];
inline int calc(const int &x,const int &y) {
if(!x) return ;
return (int64)(y*-std::min(x,y))*(std::min(x,y)-)/;
}
int main() {
int n=getint(),m=getint(),b=getint();
for(register int i=;i<=b;i++) {
const int x=getint(),y=getint();
a.push_back((Point){x,y});
}
std::sort(a.begin(),a.end());
int64 ans=;
for(register int i=;i<=n;i++) {
p.clear();
p.push_back((Point){i,});
for(register unsigned j=;j<a.size();j++) {
if(a[j].x<=i) {
p.push_back(a[j]);
}
}
p.push_back((Point){i,m+});
while(!q.empty()) q.pop();
q.push();
for(register unsigned i=;i<p.size();i++) {
while(q.size()>&&p[q.top()].x<=p[i].x) q.pop();
l[i]=q.top();
q.push(i);
}
while(!q.empty()) q.pop();
q.push(p.size()-);
for(register unsigned i=p.size()-;i>;i--) {
while(q.size()>&&p[q.top()].x<p[i].x) q.pop();
r[i]=q.top();
q.push(i);
}
for(register unsigned j=;j<p.size();j++) {
ans+=calc(i,p[j].y-p[j-].y-);
}
for(register unsigned j=;j<p.size()-;j++) {
ans+=calc(i-p[j].x,p[r[j]].y-p[l[j]].y-)-calc(i-p[j].x,p[r[j]].y-p[j].y-)-calc(i-p[j].x,p[j].y-p[l[j]].y-);
}
}
printf("%lld\n",ans);
return ;
}
[SimpleOJ239]Cards的更多相关文章
- BZOJ 1004 【HNOI2008】 Cards
题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...
- Codeforces Round #384 (Div. 2) 734E Vladik and cards
E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- bzoj 1004 Cards
1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...
- codeforces 744C Hongcow Buys a Deck of Cards
C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...
- CF 204B Little Elephant and Cards
题目链接: 传送门 Little Elephant and Cards time limit per test:2 second memory limit per test:256 megab ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
- 队列 Soldier and Cards
Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...
- [CareerCup] 18.2 Shuffle Cards 洗牌
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of ...
随机推荐
- 2017-3-26 webpack入门(一)
2017-3-26 webpack入门(一) webpack 前端 打包 最近项目里用到了webpack特意总结一下.来源:http://webpackdoc.com 1 概念 1.1 webpack ...
- hdu 1081 To The Max(dp+化二维为一维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...
- Select 使用不当引发的core,你应该知道的
排查一个死机问题,搞了好几天时间,最终确定原因:最终确定问题原因,在此分享一下: 第一步:常规根据core文件查看栈信息,gdb –c core xxxx 如下rip不正确,指令地址错乱,栈信息已破坏 ...
- typeof的用法
typeof可以返回变量的类型,返回值为字符串,其值有 "undefined" "boolean" "string" "numbe ...
- 【Python学习】使用Pyinstaller将py文件导出为exe文件
PyInstaller其实就是把python解析器和你自己的脚本打包成一个可执行的文件,但是它不是跨编译的,也就是说在Windows下用PyInstaller生成的exe只能运行在Windows下,在 ...
- yocto 离线编译
使用yocto编译一个软件包时,一般会先在本地寻找下载好的源码包,如果不存在则根据配置从网络下载. 添加本地源码包 为了支持离线编译,添加一个包的配置文件后,需要在本地也准备好源码包. 可以先打开网络 ...
- hadoop中setOutputKeyClass和setOutputValueClass里类型
初学mapreduce programing,纠结一天的问题如下: job.setOutputKeyClass和job.setOutputValueClas在默认情况下是同时设置map阶段和reduc ...
- Laravel artisan commands
使用php artisan list 可以看到artisan的所有命令以及选项. 当然你也可以在此基础上扩展自己的命令. 1. key 1.1 key:generate 这是一个加密秘钥,用于保证安全 ...
- vue 同页面不同参数
项目:详情页中有一个模块为更多产品,点击也是跳转到详情页,也就是相同路由,不同参数. 试过的方法:用this.$router.push,并没有任何反应,没有任何请求,页面也未重新加载,用this.$e ...
- beego学习笔记(1)
公司准备开发一个针对塔吊行业的APP. 后台采用微服务,docker容器部署. 准备采用go进行微服务的开发. 采用beego进行restful API的开发. 第一步,访问beego的官方网站: h ...