forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillboards.cpp
More file actions
83 lines (77 loc) · 1.59 KB
/
Billboards.cpp
File metadata and controls
83 lines (77 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
vector<string> split( const string& s, const string& delim =" " ) {
vector<string> res;
string t;
for ( int i = 0 ; i != s.size() ; i++ ) {
if ( delim.find( s[i] ) != string::npos ) {
if ( !t.empty() ) {
res.push_back( t );
t = "";
}
} else {
t += s[i];
}
}
if ( !t.empty() ) {
res.push_back(t);
}
return res;
}
int W, H;
string text;
vector<string> mm;
bool isok(int s) {
int mx = H / s, c = 1, t = 0;
REP(i,mm.size()) {
int len = mm[i].length() * s;
int add = len;
if (i != 0) add += s;
if (t + add <= W) t += add;
else {
if (len > W) return false;
++c;
if (c > mx) return false;
t = len;
}
}
return true;
}
void run() {
cin >> W >> H;
getline(cin, text);
mm = split(text, " ");
int l = 1, r = min(W, H);
while (l <= r) {
int m = (l + r) / 2;
if (isok(m)) {
l = m + 1;
} else {
r = m - 1;
}
}
cout << l - 1 << endl;
}
int main() {
int kase;
cin >> kase;
FOR(k,1,kase) {
cout << "Case #" << k << ": ";
run();
}
return 0;
}