ZOJ 3706 Break Standard Weight 解题报告

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009

题目意思:给出两个mass:x 和 y,问如何将其中一个 mass 一分为二(当然分完之后它们的和要等于原来的mass,或x 或
y),使得利用这三个mass 可称的数量最大。输出这个最大数量。

网上参考别人用STL中的set来写,太厉害了!!!考虑到set对于重复的元素只存储一个,那么当三个mass组合的过程中有重复的,它都会自动舍弃有重复的,不需要用if来判断!另外,也有可能其中两个mass是相等的,这时如果各放一边,就只会称到0,即不能称到任何物体!所以预先把0插入,这就是代码中为什么最后要返回
 st.size()-1 的原因!!


 1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cmath>
5 #include <set>
6 using namespace std;
7
8 set<int> st;
9 int cal(int a, int b, int c)
10 {
11 st.clear(); // 每一次都要清空集合里的元素
12 st.insert(0); // 考虑到左右两边各有一个相同mass的情况,这时称不了物体!
13 st.insert(a); // 只选一个mass称物体
14 st.insert(b);
15 st.insert(c);
16
17 st.insert(a+b); // 只选两个mass称另外一边的物体
18 st.insert(abs(a-b));
19 st.insert(b+c);
20 st.insert(abs(b-c));
21 st.insert(a+c);
22 st.insert(abs(a-c));
23
24 st.insert(abs(a+b-c)); // 两个mass在一边,另一边放第三个mass和代称物
25 st.insert(abs(a+c-b));
26 st.insert(abs(b+c-a));
27
28 st.insert(a+b+c); // 三个mass在一边,另一边称物体
29 return st.size()-1;
30 }
31
32 int main()
33 {
34 int T, x, y, sum;
35 while (scanf("%d", &T) != EOF)
36 {
37 while (T--)
38 {
39 sum = 0;
40 scanf("%d%d", &x, &y);
41 for (int i = 1; i <= x; i++)
42 {
43 int t1 = i;
44 int t2 = x-i;
45 int t3 = y;
46 sum = max(sum, cal(t1, t2, t3));
47 }
48 for (int i = 1; i <= y; i++)
49 {
50 int t1 = i;
51 int t2 = y-i;
52 int t3 = x;
53 sum = max(sum, cal(t1, t2, t3));
54 }
55 printf("%d\n", sum);
56 }
57 }
58 return 0;
59 }

ZOJ 3706 Break Standard Weight 解题报告,码迷,mamicode.com

时间: 2024-08-05 11:11:57

ZOJ 3706 Break Standard Weight 解题报告的相关文章

zoj 3706 Break Standard Weight(dp)

Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 KB The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length ar

ZOJ 3706 Break Standard Weight (模拟题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3706 题意: 给你两个标准重量的物体(质量为整数),你可以选择将其中一个分成两块(整数),求用这三个物体最多可以称出多少种重量. 其实一共就13种情况,数据量又比较小,用set排除一下重复的,注意下0就好. 代码: #include <iostream> #include <cstdio> #include <set> #include

zoj 3706 Break Standard Weight

分治的思想,比较水,但题很好 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,n,m; int vis[50010]; void v(int a,int b) { vis[a]=1;vis[b]=1; vis[a+b]=1;vis[abs(a-b)]=1; } int rmax(int a,int b,int c) { int rcount=0; mems

zoj 2313 Chinese Girls&#39; Amusement 解题报告

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313 题目意思:有 N 个人(编号依次为1~N)围成一个圆圈,要求求出最大的 K (1 ≤ K ≤ N/2),表示从编号为1的人开始,将球传递给他后一个人数起的第K个人,第K个人又传递给往后数的第K个人......要求这样传递下去,且每个人都有机会接到球.也就是不存在当未使得全部人都接到一次球的情况下,某个人接收到两次以上的球. 详细的解题报告在这里: http:/

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

ZOJ Monthly, June 2014 解题报告

A.Another Recurrence Sequence B.Gears 题目大意:有n个齿轮,一开始各自为一组,之后进行m次操作,包括以下4种类型: 1.合并两组齿轮,合并的两个应该反向旋转 2.把某个齿轮从所在组删除,自为一组,但不影响同组其它齿轮的状态与关系 3.询问两个齿轮是同向.反向或无关系(即不在同一组) 4.询问某个齿轮所在组的齿轮总数 分析:典型的并查集操作,但是注意两点: 1.由于操作3要询问两个齿轮的相对状态,因此对并查集中每个元素应当保存它的状态信息.状态是相对的,只需要

pat解题报告【1074】

1074. Reversing Linked List (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→

杭州电子科技大学Online Judge 之 “确定比赛名次(ID1285)”解题报告

杭州电子科技大学Online Judge 之 "确定比赛名次(ID1285)"解题报告 巧若拙(欢迎转载,但请注明出处:http://blog.csdn.net/qiaoruozhuo) Problem Description 有N个比赛队(1<=N<=500).编号依次为1,2,3,.....N进行比赛.比赛结束后.裁判委员会要将全部參赛队伍从前往后依次排名. 但如今裁判委员会不能直接获得每一个队的比赛成绩,仅仅知道每场比赛的结果.即P1赢P2,用P1.P2表示,排名时P

最小生成树,POJ和HDU几道题目的解题报告(基于自己写的模板)

首先POJ题目: 链接:1251 Jungle Roads 题目大意:纯求最小生成树,结果为最小权值边的和.采用邻接表 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 #define maxn 30 //最大顶点个数 8 int n; //顶点数,边数 9 10 struct arcn