【HDU - 1937 】Finding Seats(二维前缀和+尺取法)
題干:
A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem instead of going on with informal arguments to decide which tickets to buy.?
 The movie theater has R rows of C seats each, and they can see a map with the currently available seats marked. They decided that seating close to each other is all that matters, even if that means seating in the front row where the screen is so big it’s impossible to see it all at once. In order to have a formal criteria, they thought they would buy seats in order to minimize the extension of their group.?
 The extension is defined as the area of the smallest rectangle with sides parallel to the seats that contains all bought seats. The area of a rectangle is the number of seats contained in it.?
 They’ve taken out a laptop and pointed at you to help them find those desired seats.?
Input
Each test case will consist on several lines. The first line will contain three positive integers R, C and K as explained above (1 <= R,C <= 300, 1 <= K <= R × C). The next R lines will contain exactly C characters each. The j-th character of the i-th line will be ‘X’ if the j-th seat on the i-th row is taken or ‘.’ if it is available. There will always be at least K available seats in total.?
 Input is terminated with R = C = K = 0.?
Output
For each test case, output a single line containing the minimum extension the group can have.
Sample Input
3 5 5 ...XX .X.XX XX... 5 6 6 ..X.X. .XXX.. .XX.X. .XXX.X .XX.XX 0 0 0Sample Output
6 9題目大意:
讓你找到最小的矩形體積,滿足在他范圍內存在至少k個 ' . '
解題報告:
? ? 這題思路比較詭異。
AC代碼:
#include<bits/stdc++.h>using namespace std; const int INF =0x3f3f3f3f; int minn; char tmp[1000 + 5][1000 + 5]; int maze[1000 + 5][1000 + 5]; int main() {int r,c,k;while(~scanf("%d%d%d",&r,&c,&k) ) {if(r+c+k == 0) break;memset(maze,0 ,sizeof(maze));minn = INF;for(int i = 1; i<=r; i++) {scanf("%s",tmp[i]+1);}for(int i = 1; i<=r; i++) {for(int j = 1; j<=c; j++) {tmp[i][j] == '.' ? maze[i][j] = 1 : maze[i][j] = 0;}} // for(int i = 1; i<=r; i++) { // for(int j = 1; j<=c; j++) { // printf("%d",maze[i][j]); // } // cout<<endl; // }for(int i = 1; i<=r; i++) {for(int j = 1; j<=c; j++) {maze[i][j] = maze[i][j] + maze[i-1][j] + maze[i][j-1] - maze[i-1][j-1];}} // for(int i = 1; i<=r; i++) { // for(int j = 1; j<=c; j++) { // printf("%d",maze[i][j]); // } // cout<<endl; // }int ll,rr;for(int i = 1; i <= r; i++)for(int j = i; j <= r; j++) {ll = 1; rr = 1;while(ll<=c && rr <=c) {while(maze[j][rr] - maze[j][ll-1] - maze[i-1][rr] + maze[i-1][ll-1]>=k && ll<=rr) {minn = min(minn,(j-i+1)*(rr-ll+1) );ll++;}rr++;}}printf("%d\n",minn);}return 0 ; }?總結:
? ? ? 這道題難度也不能說大吧但是比較綜合,這年頭o(n^3)的方法基本不敢想。兩個小地方出了錯一個是二維前綴和的處理上寫成了maze[i][j] = maze[i][j] - maze[i-1][j] - maze[i][j-1] + maze[i-1][j-1]; ?第二個錯是在第二個while里面應該先更新minn再ll++,而不是先ll++再更新minn。
? ? ? 把符合要求的點抽象成1,不符合的抽象成0,這一技巧貌似很常用?
總結
以上是生活随笔為你收集整理的【HDU - 1937 】Finding Seats(二维前缀和+尺取法)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 招行信用卡账单分期可以提前还款吗 招行信
- 下一篇: 第一次办信用卡哪家好办 哪家银行信用卡好
