A题:TubeTube Feed (codeforces.com)
签到题嘛,简述一下题干,mushroom吃午饭不超过t秒,tubetube给一个n个视频的列表,索引从1到n,第i个视频持续ai秒,具有价值bi,当打开一个视频时,可以花费1秒时间跳到下一个视频(如果存在),可以跳过任何次数,包括零次
要求:帮助mushroom找到一个可以在t秒内打开并看完的视频,如果有多个,选择具有价值最高的一个,返回选取视频的索引,如果没有,则返回-1
思路:
从第一个开始,依次判断能否看完,如果能,则与当前最大价值进行比较,最后得到的即为能看完且价值最大的视频索引(初始化为-1,如果都不能看完,则自动返回-1)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int q;
cin >> q;
while (q--)
{
int n, t;
cin >> n >> t;
int a[55], b[55];
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
int num = -1, maxx = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] + i - 1 <= t && maxx < b[i])
{
maxx = b[i];
num = i;
}
}
cout << num << '\n';
}
return 0;
}
B题:Karina and Array (codeforces.com)
也是签到题,题干就是给一个数组a,可以从数组中移除任意个元素,并且可以多次移除,要找到移除后两个相邻元素的最大乘积。
思路:直接从数组中选出来最大元素和第二大元素以及最小和第二小的元素(负负得正),然后相乘选出其中最大的值就可以
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int max1 = -1e9, max2 = -1e9, min1 = 1e9, min2 = 1e9;
while (n--)
{
int a;
cin >> a;
if (a > max1)
{
max2 = max1;
max1 = a;
}
else if (a > max2)
{
max2 = a;
}
if (a < min1)
{
min2 = min1;
min1 = a;
}
else if (a < min2)
{
min2 = a;
}
}
cout << max((long long)max1 * max2, (long long)min1 * min2) << '\n';
}
return 0;
}
找规律的题,看着样例推一下,ans = n * (n + 2) + 2
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
cout << (long long)n * (n + 2) + 2 << '\n';
}
return 0;
}
这道题,当时比赛的时候是看着样例猜出来的规则,就,可以说比较懵,然后写题解之前看了看大佬们的思路,感觉对这道题有了一个理解,参考如下:
Codeforces Round 867 (Div. 3)(CF1822)A-D题解 - 知乎 (zhihu.com)
Codeforces Round 867 (Div. 3) A - G - 知乎 (zhihu.com)
将这两点结合起来可得奇数不可能和偶数情况下的排列方式,
代码如下
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
if (n % 2 == 1)
if (n == 1)
cout << "1\n";
else
cout << "-1\n";
else
{
int a = n - 1;
int b = n;
int t = n / 2;
while (t--)
{
cout << b << ' ' << a << ' ';
a -= 2;
b += 2;
b %= n;
}
cout << '\n';
}
}
return 0;
}
这道题属于是要求比较细心的一类题?通过思考,我们可以想到当不同字母构成的回文对互换为最短操作数,并且,可以确定当某一字母大于总数的一半时无解(一定有回文对),其他情况下一定有解,所以,在有解的情况下,我们的最小操作数为由出现次数最多的字母构成的回文对与其他字母构成的回文对互换,如果次数最多的字母大于其他字母的个数,则交换次数为次数最多字母出现次数,否则,为总回文对数/2,向上取整
#include <bits/stdc++.h>
using namespace std;
int chong[26], num[26];
int maxx_xia;
int main()
{
int t;
cin >> t;
while (t--)
{
memset(num, 0, sizeof(num));
memset(chong, 0, sizeof(chong));
maxx_xia = 0;
int n;
string s1, s;
cin >> n >> s1;
s = " " + s1;
if (n % 2 == 1)
cout << "-1\n";
else
{
for (int i = 1; i <= n / 2; i++)
{
if (s[i] == s[n - i + 1])
{
chong[(int)(s[i] - 'a')]++;
}
num[(int)(s[i] - 'a')]++;
num[(int)(s[n - i + 1]) - 'a']++;
}
bool jud = 0;
int num_ans = 0;
for (int i = 0; i < 26; i++)
{
if (num[i] > n / 2)
{
jud = 1;
break;
}
num_ans += chong[i];
if (chong[i] > chong[maxx_xia])
{
maxx_xia = i;
}
}
if (jud)
{
cout << "-1\n";
continue;
}
int ans = 0;
if (num_ans < chong[maxx_xia] * 2)
{
num_ans = chong[maxx_xia];
}
else
num_ans = ceil((double)num_ans / 2);
cout << num_ans << '\n';
}
}
return 0;
}
看F题的时候,离结束比赛已经不远了,所以就摆烂了,这道题是后来补的,这道题的思路如下:
首先分析思路,最终选择的或为两个叶节点之间的路径,或为从根节点到最深叶节点的路径.
当转移根代价大于收益时,直接让结果为根节点到最深节点的路径.
其他情况时,我们考虑到这两个叶节点肯定包含最深的叶节点(最优),所以我们可以遍历叶节点的路径,取对应父节点的最深和第二深的路径作为两个叶节点,与当前最优结果比较,最后再与从根节点到最深节点的路径比较.
代码如下:
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
struct node
{
int ceng, first = 0, second = 0, fa = -1;
vector<int> link;
} nod[200005];
bool jud[200005];
int maxx_xia, maxx;
vector<int> nextt[200005];
void real_init(int now)
{
jud[now] = 1;
for (int i = 0; i < nextt[now].size(); i++)
{
if (!jud[nextt[now][i]])
{
nod[now].link.push_back(nextt[now][i]);
nod[nextt[now][i]].fa = now;
}
}
for (int i = 0; i < nextt[now].size(); i++)
{
if (!jud[nextt[now][i]])
real_init(nextt[now][i]);
}
}
int init(int root, int ceng)
{
nod[root].ceng = ceng;
if (ceng > maxx)
{
maxx = ceng;
maxx_xia = root;
}
for (int i = 0; i < nod[root].link.size(); i++)
{
nod[root].second = max(nod[root].second, init(nod[root].link[i], ceng + 1));
if (nod[root].first < nod[root].second)
{
int t = nod[root].second;
nod[root].second = nod[root].first;
nod[root].first = t;
}
}
return nod[root].first + 1;
}
int main()
{
int t;
cin >> t;
// time_t start = clock(), end;
while (t--)
{
jud[1] = 1;
maxx = 0;
int n, k, c;
scanf("%d %d %d", &n, &k, &c);
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%d %d", &a, &b);
nextt[a].push_back(b);
nextt[b].push_back(a);
}
real_init(1);
long long ans = 0;
init(1, 0);
if (k <= c)
{
ans = (long long)nod[1].first * k;
}
else
{
while (maxx_xia != 1)
{
ans = max(ans, (long long)nod[maxx_xia].first * k + (long long)nod[maxx_xia].second * (k - c) - (long long)nod[maxx_xia].ceng * c);
maxx_xia = nod[maxx_xia].fa;
}
ans = max(ans, (long long)nod[maxx_xia].first * k + (long long)nod[maxx_xia].second * (k - c) - (long long)nod[maxx_xia].ceng * c);
ans = max(ans, (long long)nod[1].first * k);
}
cout << ans << '\n';
for (long long i = 1; i <= n; i++)
{
jud[i] = 0;
nod[i].first = nod[i].second = 0;
nod[i].link.clear();
nextt[i].clear();
}
}
// end = clock() - start;
// cout << end;
return 0;
}
G1和G2是搜的题解
Codeforces Round 867 (Div. 3) A - G - 知乎 (zhihu.com)
Codeforces Round 867 (Div. 3) A - G2 - 知乎 (zhihu.com)
放上佬们的链接
G1的话,主要在于对复杂度的计算上,思路如果能够确定复杂度足够,还是挺容易想出来的吧
G2的话,只能说,值域分治nb,高级操作,在x小于1e6的时候,枚举x所有因子,复杂度为sqrt(x),在x>1e6时,因为最高值为1e9,所以只需要判断从1-1e3的因子,复杂度同样为1000,高级操作