日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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],每本書的高度相同。現(xiàn)在想要制作一個(gè)書架,為了節(jié)省材料,盡量將書豎著放,剩下的書可以橫著放在之前豎著的書的上面(當(dāng)然寬度的那個(gè)面朝自己)。求書架的最小寬度。

解題報(bào)告:

因?yàn)橛质欠殖闪藘山M,所以又是一道組內(nèi)貪心組間dp。跟這題很像

思路是這樣的:考慮答案,肯定是一些寬度1的和一些寬度2的。而放置下面的書的時(shí)候肯定是寬度大的先放,so先組內(nèi)排個(gè)序,然后暴力枚舉放在下面的第一組的數(shù)量和第二組的數(shù)量,用交換法可以證明貪心的正確性。模擬完題意之后,看是否滿足要求,滿足就更新ans。

注意求前綴和不能讀入的時(shí)候求,,別偷懶,,因?yàn)槟菚r(shí)候還沒排序呢、、、

這題也可以枚舉down的長(zhǎng)度,然后貪心填充,然后check一下是否up和down是否滿足就可以了。當(dāng)然check函數(shù)中還要暴力枚舉選擇的寬度為1的個(gè)數(shù)(也就是On的check),反正還不如第一種方法簡(jiǎn)單。

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; }

?

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 294B】Shaass and Bookshelf(枚举,贪心,思维,组内贪心组间dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。