题目大意:
  有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的更多相关文章

  1. BZOJ 1004 【HNOI2008】 Cards

    题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...

  2. 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 ...

  3. bzoj 1004 Cards

    1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...

  4. 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 ...

  5. CF 204B Little Elephant and Cards

    题目链接: 传送门 Little Elephant and Cards time limit per test:2 second     memory limit per test:256 megab ...

  6. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  7. 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 ...

  8. 队列 Soldier and Cards

    Soldier and Cards 题目: Description Two bored soldiers are playing card war. Their card deck consists ...

  9. [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 ...

随机推荐

  1. kolakoski序列

                   搜狐笔试=.= 当时少想一个slow的指针..呜呜呜哇的一声哭出来 function kolakoski(token0, token1) { token0 = token ...

  2. this可以通过call改变的测试

  3. 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week1 Introduction to deep learning课堂笔记

    Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week1 Introduction to deep learn ...

  4. linux驱动基础系列--Linux下Spi接口Wifi驱动分析

    前言 本文纯粹的纸上谈兵,我并未在实际开发过程中遇到需要编写或调试这类驱动的时候,本文仅仅是根据源码分析后的记录!基于内核版本:2.6.35.6 .主要是想对spi接口的wifi驱动框架有一个整体的把 ...

  5. 流程控制--if条件

    /* if ....else .... */ [root@localhost test1]# vim .py //ADD #!/usr/bin/python >: print 'hello py ...

  6. VMware Workstation虚拟机Ubuntu中实现与主机共享(复制和粘贴)

    VMware Workstation中安装虚拟机Ubuntu后,开始都不能与主机实现共享,即相互之间能实现复制粘贴的功能.要解决问题,只需要安装VMvare tools后然后重启虚拟机Ubuntu即可 ...

  7. LinkedList 源码分析

    LinkedList :双向链表结构, 内部存在frist节点 和 last节点.通过改变 首节点和 尾节点的引用来实现新增和修改 有一个内部类: //节点类,内部包括前节点和后节点,和数据项 // ...

  8. C#判断目录是否为隐藏

    判断方法: DirectoryInfo di = new DirectoryInfo(path); if ((di.Attributes & FileAttributes.Hidden) == ...

  9. Html Css  练习

    一.  取消a链接的下划线 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  10. linux命令(14):ifup/ifdown/ip addr命令

    开启网卡:ifup eth0 关闭网卡:ifdown eth0 查看网卡接入状态:ip addr[可查看哪块网卡up/down状态]