您的位置:首页 > 其它

该暴力的时候还是要暴力滴!

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 1
output
1 1 1-1
input
4 31 2 3 1
output
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);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐