UVA, 102 Ecological Bin Packing

题意:给你仨箱子,每个箱子里面有三种瓶子B G C,让你将其分类,使得每个箱子里只有一种瓶子,

   每次只能移动一个瓶子,要求移动步数最小,相同则按字典序输出

思路:排列得到顺序有:BCG BGC CBG CGB GBC GCB 六种,都算出来按大小找就行了

   ps:输入时顺序为:B G C ,为了方便我排序成了 B C G

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3
 4 using namespace std;
 5 int date[3][3],sum[6];
 6 int mins,p;
 7 string type[6]={"BCG","BGC","CBG","CGB","GBC","GCB"};
 8 bool datecin()//每个箱子瓶子B C G
 9 {
10     if(scanf("%d%d%d%d%d%d%d%d%d",&date[0][0],&date[0][2],&date[0][1],&date[1][0],&date[1][2],&date[1][1],&date[2][0],&date[2][2],&date[2][1])!=EOF)
11         return true;
12     return false;
13 }
14
15 void datecal()
16 {
17
18     sum[0]=date[1][0]+date[2][0]+ date[0][1]+date[2][1]+ date[0][2]+date[1][2];//BCG
19     sum[1]=date[1][0]+date[2][0]+ date[0][2]+date[2][2]+ date[0][1]+date[1][1];//BGC
20
21     sum[2]=date[1][1]+date[2][1]+ date[0][0]+date[2][0]+ date[0][2]+date[1][2];//CBG
22     sum[3]=date[1][1]+date[2][1]+ date[0][2]+date[2][2]+ date[0][0]+date[1][0];//CGB
23
24     sum[4]=date[1][2]+date[2][2]+ date[0][0]+date[2][0]+ date[0][1]+date[1][1];//GBC
25     sum[5]=date[1][2]+date[2][2]+ date[0][1]+date[2][1]+ date[0][0]+date[1][0];//GCB
26
27     mins=sum[0],p=0;
28     for(int i=1;i<6;i++) if(mins>sum[i]) { mins=sum[i],p=i;}
29 }
30
31 void showres()
32 {
33     datecal();
34     cout<<type[p]<<‘ ‘<<sum[p]<<endl;
35 }
36
37 int main()
38 {
39     while(datecin())
40     {
41         showres();
42     }
43     return 0;
44 }
时间: 2024-10-10 04:29:46

UVA, 102 Ecological Bin Packing的相关文章

【习题 8-1 UVA - 1149】Bin Packing

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个背包只能装两个东西. 而且每个东西都要被装进去. 那么我们随意考虑某个物品.(不必要求顺序 这个物品肯定要放进某个背包里面的. 那么背包数递增. 那么剩余的空间. 只能装一个了. 要装谁呢? 肯定是尽可能装较大的.所以用upper_bound-1找一个最大的能装的装就可以了. 这样就能尽量减少体积较大的物品了. [代码] /* 1.Shoud it use long long ? 2.Have you ever test s

POJ 2782 Bin Packing

 Bin Packing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5416   Accepted: 2452 Description A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l and each item i has length li<=l

UVA 1149 Bin Packing

传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l and each item i has length li ≤ l. We look for a minimal number of bins q such that • each bin contains at most 2 items, • each item is pa

UVA 1149 Bin Packing 二分+贪心

A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samelength l and each item i has length li ≤ l. We look for a minimal number of bins q such that• each bin contains at most 2 items,• each item is packed in

uva 1149:Bin Packing(贪心)

题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 100100 int a[N]; int main() { int t; scanf("%d", &a

UVA 1149 Bin Packing 装箱(贪心,水)

每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+42; int a[maxn]; template<class T> inline void scan_d(T *ret) { char c;*ret=0; while((c=getchar())<'0'||c>'9'); while(c>='0'&a

UVa 1149 Bin Packing 题解

难度:α 用时:25 min 题目:?? 代码:?? 这是一道赤裸裸的贪心题.赤裸到和模版题差不多了. 所以下面长话短说. 朴素的算法是直接统计. 在这之上因为要贪心,所以做点小手段,记录一下可以被连带走的物体. 1 for (int i = 0; i < n; i++) { 2 if (taken[i]) continue; 3 ans++; 4 int iter = upper_bound(l, l+n, L-l[i]) - l; iter--; 5 while (iter > i &am

Bin Packing 贪心

F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l<te

POJ2782:Bin Packing

Description A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l<tex2html_verbatim_mark> and each item i<tex2html_verbatim_mark> has length lil<tex2html_ve