该暴力的时候还是要暴力滴!
2014-11-02 22:12
162 查看
B. Cinema Cashiertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAll cinema halls in Berland are rectangles with K rows of K seatseach, and K is an odd number. Rows and seats are numbered from 1to K.For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majorityof Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people,who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program choosesseats and sells tickets. As the request for M seats comes, the program should determine the row number x andthe segment [yl, yr] ofthe seats numbers in this row, where yr - yl + 1 = M.From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say, —the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is .If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower).If the variants are still multiple, it should choose the one with the minimum yl.If you did not get yet, your task is to simulate the work of this program.InputThe first line contains two integers N and K (1 ≤ N ≤ 1000, 1 ≤ K ≤ 99)— the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi fromthe range [1, K] — requests to the program.OutputOutput N lines. In the i-th line output «-1» (withoutquotes), if it is impossible to find Mi successiveseats in one row, otherwise output three numbers x, yl, yr.Separate the numbers with a space.Sample test(s)input9fbd
2 11 1output
1 1 1-1input
4 31 2 3 1output
2 2 21 1 23 1 32 1 1
用此方法时间复杂度会达到O(n^4),但是由题意可知1<K<99,故该题可暴力枚举
#include<cstdio>#include<cmath>#include<iostream>using namespace std;#define MAX 1005#define T printf("***\n");#define arr0 memset(arr,0,sizeof(arr));#define arr1 memset(arr,-1,sizeof(arr));#define for_i for(i=0;i<n;i++)#define for_j for(j=0;j<n;j++)#define pf printf#define cf scanfint n,k,val[105][105],map[105][105];void search(int m){int i,j,v;int min=1000*1000,row=0,left=0;for(i=1;i<=k;i++)for(j=1;j<=k-m+1;j++){int ans=0,flag=0;for(v=0;v<m;v++)if(map[i][j+v]){flag=1;break;}if(!flag){for(v=0;v<m;v++)ans+=val[i][j+v];if(ans<min){min=ans;row=i;left=j;}}}if(!row) pf("-1\n");else{for(v=0;v<m;v++)map[row][left+v]=1;pf("%d %d %d\n",row,left,left+m-1);}}int main(){int i,j,mid,m;cf("%d%d",&n,&k);mid=k/2+1;memset(map,0,sizeof(map));for(i=1;i<=k;i++)for(j=1;j<=k;j++)val[i][j]=abs(i-mid)+abs(j-mid);for(i=0;i<n;i++){cf("%d",&m);search(m);}}
相关文章推荐
- CF 633 D 离散化+二分查找+暴力(还是要多学习学习呀,挺好的题目)
- HDU 5952 Counting Cliques(我对暴力的理解还是不狗)
- 大家调试ZEND的时候应该会用到zend studio,不知道是不是中国购买zend产品的用户太少了还是我安装的问题,
- 空暇时候思考2('\0'等价于数字0还是字符0)
- 在我还是14岁的时候那会学C++
- 面临失败的时候,不能饥不择食,还是要从长远考虑才行
- iOS中代码创建UIView的时候,使用weak还是strong?
- 战争狂还是理想主义者——人类暴力迷思
- 关于cocos2d-x项目移植到android平台的时候明明已经将资源复制到assets还是出现的assets is nullptr的问题
- 快笑死,每个人出生的时候,身体都会有“金木水火土雷风”这几种属性之一的特性——不过在领悟天道成了神之后,成神之人的属性就会消失,再无任何区别。我的特性还是在于做工程
- sql server 2008安装的时候选NT AUTHORITY\NEWORK SERVICE 还是选 NT AUTHORITY\SYSTEM ?
- struts做开发的时候action是继承Action方法还是ActionSupport方法
- 2015/7/27 (主力流出-1200亿,上周五回踩,今天到底是震荡下行,还是红魔呢?——在周五成功逃顶,结果今天回调的时候被套!——教训!)
- 判断扫码的时候是支付宝还是微信
- Apache的ANT Project的主页访问的时候总是出现问题,还是把它的镜像站点记下来的好!
- Android 开发,应用的图标已经换了,运行的时候还是默认的安卓机器人
- 将数据放入到session域中,还是访问的时候一直使用request进行访问数据库
- 热爱工作 发财机会大增(这里不是选择软件还是硬件的问题,是自己的性格和追求的问题)——当你的老板不如你懂行的时候,还赚的盆满钵满的时候,你就可以考虑独立了 good
- OSChina 周六乱弹 ——程序员还是大学生的时候 带啥去上学
- POP3和IMAP4登录的时候老是提示密码,但是输入密码还是不能登录!