题干:
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.
Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
Input
The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next nlines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Examples
Input
5 1 12 1 3 2 15 2 5 2 1
Output
5
Input
3 1 10 2 1 2 4
Output
3
题目大意:
给出n本书,每本书有厚度t[i]和书的宽度w[i],每本书的高度相同。现在想要制作一个书架,为了节省材料,尽量将书竖着放,剩下的书可以横着放在之前竖着的书的上面(当然宽度的那个面朝自己)。求书架的最小宽度。
解题报告:
因为又是分成了两组,所以又是一道组内贪心组间dp。跟这题很像
思路是这样的:考虑答案,肯定是一些宽度1的和一些宽度2的。而放置下面的书的时候肯定是宽度大的先放,so先组内排个序,然后暴力枚举放在下面的第一组的数量和第二组的数量,用交换法可以证明贪心的正确性。模拟完题意之后,看是否满足要求,满足就更新ans。
注意求前缀和不能读入的时候求,,别偷懒,,因为那时候还没排序呢、、、
这题也可以枚举down的长度,然后贪心填充,然后check一下是否up和down是否满足就可以了。当然check函数中还要暴力枚举选择的宽度为1的个数(也就是On的check),反正还不如第一种方法简单。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX=105;
int n;
int d1[MAX],d2[MAX],sum1[MAX],sum2[MAX];
int main()
{int t,q;int op,w,c1=0,c2=0;int s1,s2,ans=0x3f3f3f3f;cin>>n;for(int i = 1; i<=n; i++){scanf("%d%d",&op,&w);if(op==1) d1[++c1]=w;//,sum1[c1] = d1[c1] + sum1[c1-1];else d2[++c2]=w;//,sum2[c2] = d2[c2] + sum2[c2-1];}sort(d1+1,d1+c1+1,greater<int>()); sort(d2+1,d2+c2+1,greater<int>());for(int i = 1; i<=c1; i++) sum1[i] = d1[i] + sum1[i-1];for(int i = 1; i<=c2; i++) sum2[i] = d2[i] + sum2[i-1];for(int i = 0; i<=c1; i++) {int down = 0;for(int j = 0; j<=c2; j++) {down = i+j*2;int up = (sum1[c1] - sum1[i]) + (sum2[c2] - sum2[j]);if(down >= up) ans = min(ans,down);}}printf("%d\n",ans);return 0;
}