CodeForce 841 A/B/C 解题报告

算法竞赛策略解析
本文解析了三道算法竞赛题目,包括物品分配问题、序列博弈策略和函数映射规律。通过详细阐述解题思路和代码实现,帮助读者理解如何高效解决算法问题。

A Generous Kefa

题目大意:

一共有n个物品,k个人。每一个物品都有自己的颜色,颜色相同意味着同一件物品。现在要把所有的物品分出去,如果有人拿到了相同的物品就会不高兴。问是否存在方案使得每一个人都高兴?

大致思路:

给每一个物品开一个桶,然后按照顺序把物品放到桶里,最后扫一遍桶,如果有桶里物品的数目大于人数,那么根据抽屉原理可以知道必然有有人会拿到相同的物品

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     ios::sync_with_stdio(false);
 6     int button[26];
 7     int n,k;
 8     char color[110];
 9     memset(button,0,sizeof(button));
10     cin>>n>>k;
11     cin>>color;
12     for(int i=0;i<n;++i)
13         button[color[i]-'a']++;
14     bool flag=true;
15     for(int i=0;i<26;++i)
16         if(button[i]>k){
17             flag=false;
18             break;
19         }
20     if(flag)
21         cout<<"YES"<<endl;
22     else
23         cout<<"NO"<<endl;
24     return 0;
25 }
View Code

B Godsend

题目大意:

第一个人能够拿走和为奇数的序列,第二个人能够拿走和为偶数的序列,如果轮到某人时发现序列已为空,或者没有能拿走的序列,则另一个人获胜。现在给你一个序列,问如果两个人都足够聪明,那么谁必将获胜。

大致思路:

仔细思考整个过程,可以发现;

一旦整个序列和为奇数,则第一个人可以直接把整个序列拿走然后获得胜利。所以当序列中存在奇数时,如果和是奇数则第一个人直接胜利,若是偶数,则拿走和为奇数的序列。剩下和为奇数的序列,无论第二个人怎么拿,都不会改变整个序列和的奇偶性,第一个人第二次拿的时候必定能把序列全部拿完从而获得胜利。

所以,只要序列中存在奇数,则第一个人获胜。若不存在,则第二个人获胜。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     ios::sync_with_stdio(false);
 6     int n,a;
 7     bool flag=false;
 8     cin>>n;
 9     for(int i=0;i<n;++i){
10         cin>>a;
11         if(a%2)
12             flag=true;
13     }
14     if(flag)
15         cout<<"First"<<endl;
16     else
17         cout<<"Second"<<endl;
18     return 0;
19 }
View Code

C  Leha and Function

 

这个题我纯粹看样例答案猜规律的,没想到真猜出来了,就不写思路和大意了。

发现的规律如下:比如下面序列中某一个数在下面序列中排第K个,则对应上面的序列按大小排序后的第N-K个。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=2e5+7;
 4 int a[maxn],ans[maxn];
 5 struct node{
 6     int v,id;
 7 }b[maxn];
 8 bool cmp(node a,node b)
 9 {
10     return a.v>b.v;
11 }
12 int main()
13 {
14     ios::sync_with_stdio(false);
15     int n,pos;
16     cin>>n;
17     for(int i=0;i<n;++i)
18         cin>>a[i];
19     for(int i=0;i<n;++i){
20         cin>>b[i].v;
21         b[i].id=i;
22     }
23     sort(a,a+n);
24     sort(b,b+n,cmp);
25     for(int i=0;i<n;++i){
26         pos=b[i].id;
27         ans[pos]=a[i];
28     }
29     for(int i=0;i<n;++i){
30         if(i)
31             cout<<" ";
32         cout<<ans[i];
33     }
34     cout<<endl;
35     return 0;
36 }
View Code

 

转载于:https://www.cnblogs.com/SCaryon/p/7397357.html

### Codeforces Problem 797B Explanation The problem titled "Restoring the Permutation" requires reconstructing a permutation from its prefix sums modulo \( m \). Given an array of integers representing these prefix sums, one must determine whether it is possible to restore such a permutation. In this context, a **permutation** refers to an ordered set containing each integer exactly once within a specified range. The task involves checking if there exists any valid sequence that matches the provided conditions when performing operations as described in the problem statement[^1]. To solve this issue effectively: - Iterate through all elements while maintaining two variables: `current_sum` which tracks cumulative sum during iteration; and `min_value`, used later for adjustments. ```cpp int n, m; cin >> n >> m; vector<int> s(n); for (auto& x : s) cin >> x; ``` Calculate differences between consecutive terms after adjusting initial values appropriately by subtracting minimum value found so far at every step. This adjustment ensures non-negativity throughout calculations without altering relative order among elements. Check feasibility based on properties derived from constraints given in the question text. Specifically, ensure no duplicate residues appear under modulus operation since they would violate uniqueness required for permutations. Finally, construct answer using adjusted difference list obtained previously along with necessary checks ensuring correctness according to rules outlined above.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值