CodeChef Starters 196
Cloud Watching
问的是如果早上的云是不是小于等于晚上的三倍,最后输出是Rain还是Dry
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int a,b;
cin >> a >> b;
if(b >= a * 3){
cout << "Rain" << endl;
}else{
cout << "Dry" << endl;
}
}
More Cookies
现在有C块饼干,给出一个数组A,C需要不同于A中的元素,且必须大于其中至少一个元素,问C需要最小加几才能符合条件
首先肯定得大于A的最小值,那么一开始的 C + X > Min(A)
得到new_C = C + X
然后判断new_C是不是和A里面的元素相同,如果相同,X则需要继续加,直到new_C不一样为止。
最后得到的X就是答案了。
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, c;
cin >> n >> c;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int min_a = *min_element(a.begin(), a.end());
int x = max(0, min_a - c + 1);
int new_c = c + x;
bool has_duplicate = false;
for (int num : a) {
if (num == new_c) {
has_duplicate = true;
break;
}
}
while (has_duplicate) {
x++;
new_c = c + x;
has_duplicate = false;
for (int num : a) {
if (num == new_c) {
has_duplicate = true;
break;
}
}
}
cout << x << endl;
}
return 0;
}
Sugar Limit
Chef 购买了 种不同的零食,现在需要决定吃哪些零食。
- 第 种零食的美味度为 (注意: 可以是 0 或负数),含糖量为单位。
- Chef 希望限制糖分摄入,因此他会选择一个非负整数 ,然后只能吃那些满足 的零食(即糖分不超过 单位的零食)。
- 注意:他可以选择吃或跳过任何满足 的零食,并非必须全部吃。
- 设 为 Chef 决定吃的所有零食的美味度总和。
- Chef 的满意度定义为 ,即吃的零食总美味度减去阈值 。
问题:
如果 Chef 能自由选择 和吃的零食,他的最大可能满意度是多少?
首先,可以想到应该是对Bi进行排序,最小的在前面。
其次,如果加的Ai是负数,那肯定不行,应该过滤。
最后,对于L的选择,因为L要尽可能最小,如果我们选择吃掉第i个零食,说明我们已经接受了L = Bi 这个设定
那么............结果就出来了。
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
vector<pair<int, int>> ans;
for (int i = 0; i < n; i++) {
ans.emplace_back(b[i], a[i]);
}
sort(ans.begin(), ans.end());
int res = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
int L = ans[i].first;
if(ans[i].second > 0){
sum += ans[i].second;
res = max(res, sum - L);
}
}
cout << res << endl;
}
return 0;
}
Append Average
这个就算看不懂英文,看公式也明白啥意思了。
- 选择两个不同的索引 和 (满足)。
- 计算新元素:
- 如果 是偶数,则将 ( 添加到数组 的末尾。
- 如果 是奇数,则将 ( 添加到数组 的末尾。
- 更新数组长度:每次操作后, 增加 1。
目标:
在执行完 次操作后,求数组 所有元素的最小可能总和。
首先,不管两个数加起来是啥,都可以表示为 (
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<long long> a(n);
long long sum = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
sum += a[i];
}
sort(a.begin(), a.end());
long long mn1 = a[0];
long long mn2 = a[1];
while (k > 0) {
long long temp = (mn1 + mn2 + 1) / 2;
if (temp < mn2) {
mn2 = temp;
sum += temp;
--k;
} else {
sum += temp * k;
break;
}
}
cout << sum << '\n';
}
return 0;
}
Swap Sum
有点菜了,这个不会.....题解是说简单......QWQ
Beauty in Everything
比较笼统的翻译:假定美丽串的判断是 构成矩阵后,每一列都需要包含0和1,请问一个字符串的最长的子串是多长
因为每一列都需要包含0和1,那么长度小于等于3的子串都不用考虑。
如果长度是4,那么应该有 1001 0110 0011 1100 这几种
如果长度大于4,那么至少0的数量,以及1的数量都要大于等于2,才能构造成一个美丽串
如此以来,我们就通过i和j去遍历,如果Sij是符合要求的,就使得i++,否则就是j++
这里还有一点,比如把传参写成如下形式,不然会超时.........
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
bool longestNonBeautifulSubstring(const unordered_map<char, int>& mp, const string& s, int i, int j) {
if (j - i > 3) {
if (mp.count('0') && mp.at('0') >= 2 && mp.count('1') && mp.at('1') >= 2) {
return true;
}
return false;
} else if (j - i == 3) {
string temp = s.substr(i, 4);
if (temp == "0011" || temp == "1100" || temp == "1001" || temp == "0110") {
return true;
}
return false;
}
return false;
}
void solve() {
int n;
int ans = 0;
string s;
cin >> n;
cin >> s;
unordered_map<char, int> mp;
int i = 0, j = 0;
while (j < n) {
mp[s[j]]++;
while (longestNonBeautifulSubstring(mp, s, i, j)) {
mp[s[i]]--;
i++;
}
ans = max(ans, j - i + 1);
j++;
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
GCD Reduction
给出N,X,Y。规则是 gcd(A1,A3) <= gcd(A2,A3),那么就删除A1,否则删除A2。现在要求构造数组包含1~N,且AN = X, AN-1=Y,但最后留下的数组第一位不能是Y
现在我们要构造倒数第三位MID,要保证把Y给删除,那么这个MID和X的公约数,大于Y和X的公约数。
可以想一下,
首先,只有两位肯定是不行的。
其次,gcd(Y,X) 如果刚刚好等于X,那么gcd(MID,X)一定会小于等于gcd(Y,X)......也不行。
把不能的情况判断完以后,再看看如何构造。
MID最好是X的倍数,那么gcd(MID,X)就是X,比如说MID = 2 * X,当然MID也不能超过N
如果超过N了,那么考虑MID是不是X的因数,遍历一下,如果还是不行,那么就不能构造。
假设我们能找到MID了,现在我们有AN = X, AN-1=Y, AN-2=MID。
对于排列中从第1个到第N-2个元素的每个三元组a[i], a[i+1], a[i+2],必须满足gcd(a[i], a[i+2]) ≤ gcd(a[i+1], a[i+2])
把剩下的数字先填充进去,然后去逐个交换就好~~~~
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
void solve(){
set<int> St;
int N,X,Y;
cin >> N >> X >> Y;
vector<int> a(N + 1);
St.insert(X);
St.insert(Y);
if (N == 2 || __gcd(X, Y) == X)
{
cout << -1 << "\n";
return;
}
// Y X
int mid = -1;
if(X * 2 <= N && St.count(2 * X) == 0){
mid = 2 * X;
}else{
for(int i = 2; i * i <= X; i++){
if(X % i == 0){
int a = i, b = X / i;
if(St.count(a) == 0 && __gcd(a,X) > __gcd(Y,X)){
mid = a;
break;
}
if(St.count(b) == 0 && __gcd(b,X) > __gcd(Y,X)){
mid = b;
break;
}
}
}
}
if(mid == -1){
cout << -1 << endl;
}else{
St.insert(mid);
a[N] = X;
a[N - 1] = Y;
a[N - 2] = mid;
int temp = 1;
for(int i = N - 3; i >= 1; i--){
while(St.count(temp)){
temp++;
}
a[i] = temp;
St.insert(temp);
}
for (int i = 1; i <= N - 3; i++)
{
if (__gcd(a[i], a[i + 2]) > __gcd(a[i + 1], a[i + 2]))
{
swap(a[i], a[i + 1]);
}
else
{
break;
}
}
for (int i = 1; i <= N; i++)
{
cout << a[i] << " ";
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}

浙公网安备 33010602011771号