c f 解题思路

昨天又是cf练习的一天,自己做完之后,发现有很多自己的思路,等到第二天起床就想要做点什么,也想要迫不及待记录下来,所以决定在这里留下一篇昨天的总结:

A题:

题目

A. Cards for Friends
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For the New Year, Polycarp decided to send postcards to all his n friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size w×h, which can be cut into pieces.

Polycarp can cut any sheet of paper w×h that he has in only two cases:

If w is even, then he can cut the sheet in half and get two sheets of size w2×h;
If h is even, then he can cut the sheet in half and get two sheets of size w×h2;
If w and h are even at the same time, then Polycarp can cut the sheet according to any of the rules above.

After cutting a sheet of paper, the total number of sheets of paper is increased by 1.

Help Polycarp to find out if he can cut his sheet of size w×h at into n or more pieces, using only the rules described above.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

Each test case consists of one line containing three integers w, h, n (1≤w,h≤104,1≤n≤109) — the width and height of the sheet Polycarp has and the number of friends he needs to send a postcard to.

Output
For each test case, output on a separate line:

“YES”, if it is possible to cut a sheet of size w×h into at least n pieces;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

Example
inputCopy
5
2 2 3
3 3 2
5 10 2
11 13 1
1 4 4
outputCopy
YES
NO
YES
YES
YES
Note
In the first test case, you can first cut the 2×2 sheet into two 2×1 sheets, and then cut each of them into two more sheets. As a result, we get four sheets 1×1. We can choose any three of them and send them to our friends.

In the second test case, a 3×3 sheet cannot be cut, so it is impossible to get two sheets.

In the third test case, you can cut a 5×10 sheet into two 5×5 sheets.

In the fourth test case, there is no need to cut the sheet, since we only need one sheet.

In the fifth test case, you can first cut the 1×4 sheet into two 1×2 sheets, and then cut each of them into two more sheets. As a result, we get four sheets 1×1.

思路:

如果长(w)为偶数,就可以将纸片一分为二,如果宽(h)为偶数,就可以将纸片一份为二,然后就需要判断,一张纸通过不断处理,能否分为n份。如果能输出“YES”,反之输出“NO”;然后就是需要考虑数据大小的问题,发现数据并不算太大,可以直接a过。

代码与解析:

#include <iostream>
#define ll long long
using namespace std;
void solve()
{int sum = 1;//记录纸的份数,初始化为1(开始是一张纸)int w, h, n;cin >> w >> h >> n;if (n == 1){cout << "YES" << endl;return;}while (w % 2 == 0 || h % 2 == 0){if (w % 2 == 0)//如果长为偶数,则一分为二{w /= 2;sum *= 2;//纸的份数变为二倍}if (h % 2 == 0)//如果宽为偶数,则一分为二{h /= 2;sum *= 2;//纸的份数变为二倍}if (sum >= n){cout << "YES" << endl;return;}}cout << "NO" << endl;return;
}
int main()
{int t;cin >> t;while (t--){solve();//解题函数}return 0;
}

B题:

题目:

B. Fair Division
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice and Bob received n candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice’s candies is equal to the total weight of Bob’s candies.

Check if they can do that.

Note that candies are not allowed to be cut in half.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤100) — the number of candies that Alice and Bob received.

The next line contains n integers a1,a2,…,an — the weights of the candies. The weight of each candy is either 1 or 2.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case, output on a separate line:

“YES”, if all candies can be divided into two sets with the same weight;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

思路

糖果有两类,一种1g的,一种2g的,需要将所有糖果平均分给两位同学,首先,我们判断,如果糖的总质量是奇数,无论如何都不可能平均分为两堆,那么我们只需要判断糖的总质量是偶数的,在思考,我们是不是能不能把问题简单化,分给两个同学,那么是不是只要有一个人能够得到糖果的一半,那么另一个人必然也是得到糖果的一半。

代码与解析

#include <iostream>
#include <string>
#include <algorithm>
#include <deque>
#include<vector>
#include <cmath>
#include<map>
#include<set>
#include<cstring>
#define ll long long
using namespace std;
void solve()
{int sum = 0;//记录糖果总数int sum1 = 0;//糖果质量为1g的个数int sum2 = 0;//糖果质量为2g的个数int n;cin >> n;for (int i = 1; i <= n; i++){int m;cin >> m;sum += m;//糖果质量总数if (m == 1)//糖果质量为1g的糖果{sum1++;}if (m == 2)//糖果质量为2g的糖果{sum2++;}}if (sum % 2 == 1)//如果糖果总质量为奇数,则不可能均分为两份{cout << "NO" << endl;return;}int a = sum / 2;//糖果总质量的一半,及我们所需要达到的目标int b = a / 2;//在满足糖果总质量的一半时,糖果质量为2g的个数int c = a % 2;//在满足糖果总质量的一半时,糖果质量为1g的个数//我们可以通过模拟,不断减少质量为2g的糖果的同时,增加相应质量为1g的糖果,如果存在一种情况,质量为2g的糖果盒为1g的糖果,小于等于我们所拥有的糖果数,则符合条件,反之则不能while (b >= 0){if (b <= sum2 && c <= sum1)//存在一种情况符合我们所拥有的糖果数量{cout << "YES" << endl;return;}else{b--;//质量为2g的糖果数量减1c += 2;//质量为1g的糖果数量相应增加2块}}cout << "NO" << endl;//不存在符合我们需求的情况return;
}
int main()
{int t;cin >> t;while (t--){solve();//解题函数}return 0;
}

C题

题目

C. Long Jumps
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp found under the Christmas tree an array a of n elements and instructions for playing with it:

At first, choose index i (1≤i≤n) — starting position in the array. Put the chip at the index i (on the value ai).
While i≤n, add ai to your score and move the chip ai positions to the right (i.e. replace i with i+ai).
If i>n, then Polycarp ends the game.
For example, if n=5 and a=[7,3,1,2,3], then the following game options are possible:

Polycarp chooses i=1. Game process: i=1⟶+78. The score of the game is: a1=7.
Polycarp chooses i=2. Game process: i=2⟶+35⟶+38. The score of the game is: a2+a5=6.
Polycarp chooses i=3. Game process: i=3⟶+14⟶+26. The score of the game is: a3+a4=3.
Polycarp chooses i=4. Game process: i=4⟶+26. The score of the game is: a4=2.
Polycarp chooses i=5. Game process: i=5⟶+38. The score of the game is: a5=3.
Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅105) — the length of the array a.

The next line contains n integers a1,a2,…,an (1≤ai≤109) — elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output on a separate line one number — the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from 1 to n in such a way as to maximize his result.

思路

我们通过模拟,建立一个数组存相应的输入,不断根据需要跳跃,然后选取能够取得的最大得分的步骤。但是我们就需要考虑数据大小的问题,这时就可能会时间超限,Time limit exceeded on test 3我们想要解决这个问题,就可以选择进行,记忆化搜索,在建立一个数组,存到达这一步时的最大得分,如果你从别的地方跳到这里,这时有两种情况,如果你的得分大于该点存的得分,那么你就可以继续跳跃,并更新此时的最大得分,如果你从别的地方跳到这里的得分还没有这一步的最大得分大,那么你就不需要继续跳了,这样就可以省略很大部分情况。

代码与解析

#include <iostream>
#include <string>
#include <algorithm>
#include <deque>
#include<vector>
#include <cmath>
#include<map>
#include<set>
#include<cstring>
#define ll long long
using namespace std;
void solve()
{map<int, int>m;//记忆化搜索,不重复老路,记忆到达该位置的最大得分int a[200001];//存输入的数据int n;cin >> n;for (int i = 1; i <= n; i++){int m;cin >> m;a[i] = m;}int maxx = -99;//记录最大得分,并不断更新for (int i = 1; i <= n; i++){int sum = 0;//记录从该步跳出的得分int j = i;while (j <= n){sum += a[j];//不断更新得分if (sum < m[j])//如果小于该位置的最大得分,就不需要继续跳了{break;}else{m[j] = sum;//如果大于该位置的最大得分,就更新该位置的最大分数,继续跳}j += a[j];}maxx = max(maxx, sum);//更新最大得分}cout << maxx << endl;//输出结果
}
int main()
{int t;cin >> t;while (t--){solve();//解题函数}return 0;
}

D题

题目:

D. Even-Odd Game
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
During their New Year holidays, Alice and Bob play the following game using an array a of n integers:

Players take turns, Alice moves first.
Each turn a player chooses any element and removes it from the array.
If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice’s score does not change.
Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob’s score does not change.
If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared.

For example, if n=4 and a=[5,2,7,3], then the game could go as follows (there are other options):

On the first move, Alice chooses 2 and get two points. Her score is now 2. The array a is now [5,7,3].
On the second move, Bob chooses 5 and get five points. His score is now 5. The array a is now [7,3].
On the third move, Alice chooses 7 and get no points. Her score is now 2. The array a is now [3].
On the last move, Bob chooses 3 and get three points. His score is now 8. The array a is empty now.
Since Bob has more points at the end of the game, he is the winner.
You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array.

Input
The first line contains an integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the number of elements in the array a.

The next line contains n integers a1,a2,…,an (1≤ai≤109) — the array a used to play the game.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output on a separate line:

“Alice” if Alice wins with the optimal play;
“Bob” if Bob wins with the optimal play;
“Tie”, if a tie is declared during the optimal play.

思路

就是两个朋友在一起做游戏,当然女生优先,所以就是Alice先选一个数字,然后Bob再选一个数字,先画重点“两个人都能做出最正确的选择”,那么就是一个简单的贪心问题,我们可以将所有数据排序,每个人都从最大的数字开始拿,如果Alice拿的是偶数,那么他就可以得分,如果拿的不是偶数,她不会得分,但是她会避免Bob拿到这个数字,然后就是“博弈论”,我们可以进一步简化问题,只要Alice赢那么Bob就会输,只要Alice平那么两个人就是平局。那么我们就可以只计算一个人的得分,就可以知道游戏结局。

代码与解析

#include <iostream>
#include <string>
#include <algorithm>
#include <deque>
#include<vector>
#include <cmath>
#include<map>
#include<set>
#include<cstring>
#define ll long long
using namespace std;
void solve()
{ll sum = 0;//记录Ailce的分数vector<ll>v;//存数据ll n;cin >> n;for (ll i = 1; i <= n; i++){ll m;cin >> m;v.push_back(m);}sort(v.rbegin(), v.rend());//排序for (ll i = 0; i < n; i++){if (i % 2 == 0)//因为Ailce先拿,所有当i是偶数时,是Alice拿{if (v[i] % 2 == 0)//如果该数字是偶数,那么Alice得分{sum += v[i];}}else//这时是Bob开始拿数字{if (v[i] % 2 == 1)//如果该数字是偶数,那么Bob会得分,相当于Alice扣分{sum -= v[i];}}}if (sum == 0)//Alice 不得分那么就是平局{cout << "Tie" << endl;}else if (sum > 0)//Alice 得分那么就是Alice 胜{cout << "Alice" << endl;}else//反之就是Bob胜{cout << "Bob" << endl;}
}
int main()
{int t;cin >> t;while (t--){solve();//解题函数}return 0;
}

欢迎指正!!!

codeforces解题总结:#693 (Div. 3)相关推荐

  1. Codeforces Round #693 (Div. 3)A~G解题报告

    Codeforces Round #693 (Div. 3)A~G解题报告 A Cards for Friends 原题信息 http://codeforces.com/contest/1472/pr ...

  2. Codeforces Round #693 (Div. 3)部分题解

    Codeforces Round #693 (Div. 3) 部分题解 D. Even-Odd Game 思路: 贪心:田忌赛马 (1)先将数组从大到小排序,取数时从大到小取,用一个ans变量记录取数 ...

  3. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  4. Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新

    http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...

  5. Codeforces 1005D Polycarp and Div 3

    Codeforces 1005D Polycarp and Div 3 dp[i]表示前i个数最多能分成多少块%3为0,nxt[x]表示x这个上一次出现的位置. 首先想到 $ dp[i] = max( ...

  6. Codeforces Beta Round #22 (Div. 2 Only) E. Scheme(DFS+强连通)

    题目大意 给了 n(2<=n<=105) 个点,从每个点 u 出发连向了一个点 v(共 n 条边) 现在要求添加最少的边使得整个图是一个强连通图 做法分析 这道题千万不要一般化:先求强连通 ...

  7. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)切题报告(A-B题)

    这是我第二次参加CF比赛,也是第一次写博客,写得不好,望各位大佬海涵. A. Single Wildcard Pattern Matching 原题链接 这道题,这是难到让人无法直视,太水了. 题目大 ...

  8. Codeforces Beta Round #4 (Div. 2 Only)

    Codeforces Beta Round #4 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...

  9. [区间记忆化dp入门][Bribe the Prisoners SPOJ - GCJ1C09C][Codeforces Round #505D (rated, Div. 1 + Div. 2, ba]

    Bribe the Prisoners SPOJ - GCJ1C09C 作为这类题代表,f[i][j]代表第i点到第j点单独处理的最值 这题关键:释放某个囚犯后,就把囚犯分成两段,两段互相独立 这类d ...

最新文章

  1. matlab-代数方程
  2. phpeditor编写php_在php中使用CKEDITOR在线编辑器
  3. 简陋的会计凭证金额输入控件
  4. GDCM:智能指针的测试程序
  5. RPC与其实现方式概念笔记
  6. android jni socket
  7. android 安卓editext默认弹出英文输入法,只能输入英文与数字
  8. 扩散模型初探:原理及应用
  9. 微信小程序搜索关键字高亮和ctrl+f搜索定位实现
  10. 如何编写投标项目系统设计方案?
  11. css设置弹出信息,javaScript自定义alert弹出信息窗口
  12. 自学编程,看书还是视频?
  13. [我读]十四堂人生创意课
  14. 网页内嵌lodop控件,部分电脑一直提示未安装
  15. 小提琴统计图_小提琴图解读 统计学
  16. 【学习方法】如何才能高效学习
  17. 中间件选型标准和流程
  18. 以谦逊的心态对待每一个人
  19. MySQL存emoji表情
  20. 【信号处理】CFO估计技术(Matlab代码实现)

热门文章

  1. python atm银行取款系统_银行 ATM 系统项目
  2. 2022年湖南省中医助理医师考试考前模拟题
  3. Django-Xadmin的安装和应用
  4. MMM(3M)互助平台开发
  5. 使用mysql语句进行分组查询
  6. 投放共享单车需要什么批文_浙江绍兴叫停所有共享单车:需审批才能投放
  7. LabVIEW的移位寄存器
  8. 山西移动UNT401H_hi3798mv300H_MT7668RSN(1+8)_卡刷固件
  9. 大盘点 | 基于Range-View的数据处理及3D检测算法
  10. 每位设计师都应该拥有的50个CSS代码片段