Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
题目大意:
n波人去k*k的电影院看电影。
要尽量往中间坐,往前坐。
直接枚举,贪心,能坐就坐,坐在离中心近期的地方。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 1000005
#define lowbit(x) (x&(-x)) using namespace std; struct BIT
{
int sum;
void init(){sum=0;}
}bit[105][105];
int q,n;
int Sum(int row,int x)
{
int ret=0;
for(int i=x;i>=1;i-=lowbit(i))
ret+=bit[row][i].sum;
return ret;
}
int query(int row,int l,int r)
{
return Sum(row,r)-Sum(row,l-1);
}
void update(int row,int x)
{
for(int i=x;i<=n;i+=lowbit(i))
bit[row][i].sum++;
}
int cal(int s,int e)
{
return (s+e)*(e-s+1)/2;
}
int main()
{
scanf("%d%d",&q,&n);
int cen=n/2+1; for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
{
bit[i][j].init();
}
for(int i=1;i<=q;i++)
{
int num;
scanf("%d",&num);
int ansr=-1,ansc=-1;
int min_val=0x3f3f3f3f; for(int r=1;r<=n;r++)
{
for(int c=1;c+num-1<=n;c++)
{
if(query(r,c,c+num-1)==0)
{
int tmp;
if(c>=cen)
{
tmp=cal(c,c+num-1)-cen*num+abs(r-cen)*num;
}
else if(c+num-1<=cen)
{
tmp=cen*num-cal(c,c+num-1)+abs(r-cen)*num;
}
else
{
tmp=abs(r-cen)*num+cal(cen,c+num-1)-(c+num-cen)*cen+cen*(cen-c)-cal(c,cen-1);
}
if(tmp<min_val)
{
min_val=tmp;
ansr=r;
ansc=c;
}
}
}
}
if(min_val!=0x3f3f3f3f)
{
printf("%d %d %d\n",ansr,ansc,ansc+num-1);
for(int j=ansc;j<=ansc+num-1;j++)
update(ansr,j);
}
else puts("-1");
}
return 0;
}
Codeforces Beta Round #10 B. Cinema Cashier (树状数组)的更多相关文章
- Codeforces Beta Round #10 B. Cinema Cashier 暴力
B. Cinema Cashier 题目连接: http://www.codeforces.com/contest/10/problem/B Description All cinema halls ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- Codeforces Round #365 (Div. 2) D 树状数组+离线处理
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #261 (Div. 2) D 树状数组应用
看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这种(i,j) ,i<j可是 i前面的合法个数 要大于j后面的 看起来非常像逆序数的样子 ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- codeforces 985 E. Pencils and Boxes (dp 树状数组)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)
树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且代码量和常数较小 首先定义一个数组 int c[N]; 并清空 memset(c, 0, sizeof c) ...
- Codeforces 390E Inna and Large Sweet Matrix 树状数组改段求段
题目链接:点击打开链接 题意:给定n*m的二维平面 w个操作 int mp[n][m] = { 0 }; 1.0 (x1,y1) (x2,y2) value for i : x1 to x2 for ...
随机推荐
- asp.net mvc ,asp.net mvc api 中使用全局过滤器进行异常捕获记录
MVC下的全局异常过滤器注册方式如下:标红为asp.net mvc ,asp.net mvc api 注册全局异常过滤器的不同之处 using SuperManCore; using System. ...
- php的迭代器
接口Iterator 主要需要实现的方法: abstract public mixed current ( void ) abstract public scalar key ( void ) abs ...
- select_related
作用:减少DB访问次数 from django.db import models class Blog(models.Model): name = models.CharField(max_lengt ...
- HBase性能测试
hbase org.apache.hadoop.hbase.PerformanceEvaluationUsage: java org.apache.hadoop.hbase.PerformanceEv ...
- HAMA
http://hama.apache.org/run_examples.html http://www.binospace.com/ http://57832638.iteye.com/blog/20 ...
- Python 第一章 基础知识
如果熟其他计算机语言,可能会习惯于每行以分号结束.Python则不同,一行就是一行,不管多少. 如果喜欢的话,可以加上分号,但是不会有任何作用(除非同一行还有更多的代码),而且这也不是同行的做法. & ...
- JAVA排序(二) Comparator接口
接着说关于Comparator接口, java.util Interface Comparator<T>(该泛型指定的是被比较的类),使用该接口不需要在待比较类进行比较操作,即在不修改源码 ...
- Putty远程登录VMware虚拟机Linux(Ubuntu12.04)
为了不至于来回在Win7和Ubuntu12.04之间来回切换,在Win7下使用VMware9.0安装了Ubuntu12.04. 首先下载Vmware9.0虚拟机软件,下载地址为:VMware-work ...
- 移植FreeModbus+ModbusMaster+STM32至RT-Thread(3、4阶段)
一.简介及进展 经过一个多月的努力,目前项目开发已进入最后阶段.虽然比预期时间有些延迟,但也收获不少,边工作边开源的效率确实还有待提高. 简单说下目前的进展吧 1.目前项目已经在Github中开源,大 ...
- android取得所在位置的经纬度
android提供了LocationManager来取得位置,用LocationListener来监听位置的变化 先做一些初始化工作: /** latitude and longitude of cu ...