二分——POJ3388

题目描述:

给出k种花色砖块的数量,问n*n的地板最多能铺多少行相同的。

大致思路:

n的范围40000,k的范围50000,典型的二分数据范围,这样就二分答案,每次判断能不能拼出答案即可。

代码:

#include 
#include 

using namespace std;

const int maxk = 50000 + 10;
int n,k;
int c[maxk];

int main() {
    //freopen("j.txt","r",stdin);
    while (scanf("%d%d",&n,&k) != EOF) {
        for (int i = 1; i <= k; i++) {
            scanf("%d",&c[i]);
        }
        int l = 1,r = n+1;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            int ans = 0;
            for (int i = 1; i <= k; i++) {
                ans += c[i] / mid;
                if (ans >= n) break;
            }
            if (ans >= n) l = mid;
            else r = mid;
        }
        printf("%d\n",l);
        int tmp = 0;
        for (int i = 1; i <= k; i++) {
            int tem = c[i] / l;
            if (tmp + tem <= n) {
                tmp += tem;
                for (int j = 0; j < tem; j++) printf("%d\n",i);
            }
            else {
                for (int j = 0; j < n-tmp; j++) printf("%d\n",i);
                break;
            }
        }
        //cout<


你可能感兴趣的:(二分,二分)