问题 C: Cocoa Coalition
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Alice and Bob decide to share a chocolate bar, which is an n by m rectangular grid of chocolate cells. They decide that Alice should get a < n·m pieces and that Bob should get b = n·m − a pieces. To split the chocolate bar, they repeatedly take a single piece of chocolate and break it either horizontally or vertically, creating two smaller pieces of chocolate. See Figure C.1 for an
example.
What is the minimum number of splits that Alice and Bob need to perform in order to split the n-by-m chocolate bar into two piles consisting of a and b chocolate cells?
Figure C.1: Illustration of a solution to Sample Input 2, showing the original 10-by-10 chocolate bar split three times into pieces of size 10-by-2, 10-by-5, 3-by-3 and 7-by-3. Giving Alice the 10-by-5 and 7-by-3 pieces, she gets a total of 50 + 21 = 71 chocolate cells.
输入
The input consists of a single line, containing the three integers n, m and a (1 ≤ n, m ≤ 106 ,1 ≤ a < n · m).
输出
Output the minimum number of splits needed to achieve the desired division of chocolate.
样例输入 Copy
3 10 9
样例输出 Copy
1
题目大意:
给出一个矩形的巧克力方块,每个小方块是1*1的,每次可以横着或者是竖着切一刀,问切出面积总共为 a 的一块最少需要操作几次
有一种情况是:
5 * 15 的方块,切出大小为31的一块,最少是两刀,不是三刀:
第一次切出45的一块来,然后右面剩下的事511的一块,然后正好还差 11小块,此时切一刀解决问题
Main_code:
ll n, m, a; cin >> n >> m >> a; ll _a = a; if (2 * a > n * m) a = n * m - a; if (n > m) swap(n, m); if (a % n == 0 || a % m == 0) { cout << 1 << endl; return 0; } if (a >= 1 && a < m) { cout << 2 << endl; return 0; } for (ll i = 2; i * i <= _a; i++) { if (_a % i == 0) { ll j = _a / i; if (i <= n && j <= m) { cout << 2 << endl; return 0; } } } for (ll t = 1; t <= m; t++) { ll u = n * t, v = _a - u; ll mi = m - t, mx = n; if (mi > 0 && v > 0) { if (v % mi == 0 || v % mx == 0) { cout << 2 << endl; return 0; } } else break; } for (ll t = 1; t <= n; t++) { ll u = m * t, v = _a - u; ll mi = n - t, mx = m; if (mi > 0 && v > 0) { if (v % mi == 0 || v % mx == 0) { cout << 2 << endl; return 0; } } else break; } cout << 3 << endl; return 0;
问题 G: Hot Hike
时间限制: 1 Sec 内存限制: 128 MB
题目描述
In order to pass time during your vacation, you decided to go on a hike to visit a scenic lake up in the mountains. Hiking to the lake will take you a full day, then you will stay there for a day to rest and enjoy the scenery, and then spend another day hiking home, for a total of three days. However, the accursed weather this summer is ridiculously warm and sunny, and since severe dehydration is not at the top of your priority list you want to schedule the three-day trip during some days where the two hiking days are the least warm. In particular you want to minimize the maximum temperature during the two hiking days.
Given the current forecast of daily maximum temperatures during your vacation, what are the best days for your trip?
输入
The first line of input contains an integer n (3 ≤ n ≤ 50), the length of your vacation in days. Then follows a line containing n integers t1 , t2 , . . . , tn (−20 ≤ t i ≤ 40), where ti is the temperature forecast for the i’th day of your vacation.
输出
Output two integers d and t, where d is the best day to start your trip, and t is the resulting maximum temperature during the two hiking days. If there are many choices of d that minimize the value of t, then output the smallest such d.
样例输入 Copy
5 23 27 31 28 30
样例输出 Copy
2 28
不再赘述
int minn=inf,tag=-1; int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<n-1;i++) { if(minn>max(a[i],a[i+2])) { tag=i; minn=max(a[i],a[i+2]); } } cout<<tag<<" "<<minn<<endl;
问题 F: Game of Gnomes
时间限制: 1 Sec 内存限制: 128 MB
题目描述
The enemy and their massive army is approaching your fortress, and all you have to defend it is a legion of guardian gnomes. There is no hope of winning the battle, so your focus will instead be on causing as much damage as possible to the enemy.
You have n gnomes at your disposal. Before the battle, they must be divided into at most m non-empty groups. The battle will then proceed in turns. Each turn, your gnomes will attack the enemy, causing one unit of damage for each living gnome. Then the enemy will attack by throwing a lightning bolt at one of the m groups. The lightning bolt kills k of the gnomes in that group, or all of them if the number of living gnomes in the group is less than k. The battle ends when all gnomes are dead. The enemy will always throw the lightning bolts in an optimal way such that the total damage caused by the gnomes is minimized.
Now you wonder, what is the maximum amount of damage you can cause to the enemy if you divide the gnomes into groups in an optimal way?
For example, if as in Sample Input 1 you have n = 10 gnomes that are to be divided into m = 4 groups, and the lightning bolt does at most k = 3 damage, then an optimal solution would be to create one large group of size 7 and three small groups of size 1. In the first round, you cause 10 damage and the lightning bolt reduces the large group by 3. In the next round, you cause 7 damage and the large group is reduced down to size 1. In the remaining four rounds you do 4, 3, 2, and 1 damage respectively and the lightning bolt removes one group each round. In total you do 10 + 7 + 4 + 3 + 2 + 1 = 27 damage.
输入
The input consists of a single line containing the three integers n, m, and k (1 ≤ n ≤ 109 ,1 ≤ m, k ≤ 107 ), with meanings as described above.
输出
Output the maximum amount of damage you can cause to the enemy.
样例输入 Copy
10 4 3
样例输出 Copy
27
这是一道比较不错的涉及数学的贪心构造思维题;
学长的博客讲解很详细
ll n, m, k; cin >> n >> m >> k; ll i = (n / k - m) - 10; if (i < 0) i = 0; for (; i <= n / k; i++) { if (n - i * k >= m * k) continue; ll t1 = n - (i - 1) * k; ll max1 = (n + t1) * i / 2; ll rem = n - i * k;/// 剩余的 ll tot = rem / m;/// ll modd = rem % m; ll temp1 = (rem + (m - modd) * tot + tot + 1) * modd / 2; ll temp2 = (tot + (m - modd) * tot) * (m - modd) / 2; ll ans = max1 + temp1 + temp2; res = max(res, ans); } cout << res << endl;
更新中。。。。。。