Description
在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作。公司有一些固定容 量的容器可以装这些砝码。他们想装尽量多的砝码以便搬运,并且丢弃剩下的砝码。每个容器可以装的砝码数量有 限制,但是他们能够装的总重量不能超过每个容器的限制。一个容器也可以不装任何东西。任何两个砝码都有一个 特征,他们的中总有一个的重量是另外一个的整数倍,当然他们也可能相等。 Input
第一行包含两个数n和m。表示容器的数量以及砝码的数量。(1<=n, m<=100000) 第二行包含n个整数wi,表示 每个容器能够装的最大质量。(1<=wi<=1000000000) 第三行包含m个整数mj,表示每个砝码的质量。(1<=mj<=10000 00000) Output
仅包含一个数,为能够装进容器的最多的砝码数量。 Sample Input 2 4
13 9
4 12 2 4 Sample Output 3
解题方法: 蒟蒻实在是不会做,ORZ http://hzwer.com/4761.html
#include <bits/stdc++.h>using namespace std;const int maxn = 100005;int n, m, tot, a[maxn], b[maxn], c[55], cnt[55];bool check(int x){ for(int i = x + 1; i <= tot; i++){ if(cnt[i]){ cnt[x] += c[i] / c[x]; cnt[i]--; return 1; } } return 0;}int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= m; i++) scanf("%d", &b[i]); sort(b + 1, b + m + 1); for(int i = 1; i <= m; i++){ if(b[i] != b[i-1]){ c[++tot] = b[i]; } } for(int i = 1; i <= n; i++){ for(int j = tot; j; j--){ while(c[j] <= a[i]){ a[i] -= c[j]; cnt[j]++; } } } int now = 1; for(int i = 1; i <= m; i++){ while(b[i] > c[now]) now++; if(cnt[now]) cnt[now]--; else if(check(now)) cnt[now]--; else{ PRintf("%d/n", i - 1); return 0; } if(i == m){ printf("%d/n", m); } } return 0;}新闻热点
疑难解答