【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)
題干:
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 1Output
5Input
3 1 10 2 1 2 4Output
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; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今年全球债务暴涨,美国国债增加4万亿美元
- 下一篇: 【计蒜客 - 蓝桥训练】蒜厂年会(单调队