需要检查一个包含3个整数的数组,是否存在两个数之和大于等于10。
主要思路是:
#include
using namespace std;
int main() {
int T;
cin >> T;
while(T--) {
int a[3];
for(int i = 0; i < 3; i++) {
cin >> a[i];
}
bool found = false;
int sum = accumulate(a, a + 3, 0);
for(int i = 0; i < 3; i++) {
if(sum - a[i] >= 10) {
found = true;
break;
}
}
if(found) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
return 0;
}
给出若干个(a, b)对,找出a<=10中b最大的下标
#include
typedef std::pair<int, int> pii;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
int n;
std::cin >> n;
std::vector<std::tuple<int, int, int>> a(n);
int length, quality;
for (int i = 0; i < n; i++) {
std::cin >> length >> quality;
a[i] = std::make_tuple(length, quality, i);
}
auto iter = std::partition(a.begin(), a.end(), [](auto it)
{ return std::get<0>(it) <= 10; });
auto max_iter = std::max_element(a.begin(), iter, [&](const auto &a, const auto &b)
{ return std::get<1>(a) < std::get<1>(b); });
std::cout << std::get<2>(*max_iter) + 1 << '\n';
}
}
在 8*8 的字符矩阵中有一列中存在连续小写字母存在的单词,其余皆为 *,找出其中的单词
#include
typedef std::pair<int, int> pii;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
std::vector<std::string> s(8);
for (auto &it : s)
std::cin >> it;
std::string res;
for (auto str : s)
for (char ch : str)
if(islower(ch))
res.push_back(ch);
std::cout << res << '\n';
}
}
移除尽量少的元素使得数列中所有连续的元素之差小于等于 k
#include
typedef std::pair<int, int> pii;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
int n, k;
std::cin >> n >> k;
std::vector<int> a(n);
for (auto &it : a)
std::cin >> it;
sort(a.begin(), a.end());
int res = INT_MIN;
for (int j = 0; j < n;)
{
int p = j;
while (++j < n && a[j] - a[j - 1] <= k)
;
res = std::max(res, j - p);
}
std::cout << n - res << '\n';
}
}
将 n 个边长为 a_i 的正方形,给每个正方形边长增加到 a_i + 2*w,使得所有正方形面积之和小于等于 c
#include
typedef std::pair<int, int> pii;
typedef long long LL;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
int n;
LL s;
std::cin >> n >> s;
std::vector<LL> a(n);
for (auto &it : a)
std::cin >> it;
int left = 1, right = 1e9 / 2, mid, ans;
auto check = [&](LL mid) -> bool
{
LL res = 0;
mid <<= 1;
for (auto it : a)
{
res += (mid + it) * (mid + it);
if (res > s)
return false;
}
return true;
};
while (left <= right)
{
mid = (left + right) >> 1;
if (check(mid))
{
left = mid + 1;
ans = mid;
}
else
right = mid - 1;
}
std::cout << ans << '\n';
}
}
有 n 只青蛙,每只青蛙以 a_i 为步长不停得跳,在 [1, n] 中设置一个陷阱,捕捉尽可能多得青蛙,问做多能抓几只青蛙
复杂度为[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iUrS9HAN-1690212116810)(https://cdn.nlark.com/yuque/__latex/e45d75731139a9fda4a629426bf1f9ac.svg#card=math&code=n%2Alog%28n%29&id=Y0WpK)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y9BuBzYY-1690212116812)(https://cdn.nlark.com/yuque/__latex/1628a4ea7644802f0675c699b8e8320e.svg#card=math&code=%5Cfrac%7Bn%7D%7B1%7D%2B%5Cfrac%7Bn%7D%7B2%7D%2B%5Ccdots%2B%5Cfrac%7Bn%7D%7Bn%7D&id=EnBuL)] 这是著名的调和级数等于 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zoWblqHE-1690212116813)(https://cdn.nlark.com/yuque/__latex/dfbe0f7fcfb94e894b5c8502532f2797.svg#card=math&code=n%2Aln%28n%29&id=rscJV)]
#include
typedef std::pair<int, int> pii;
typedef long long LL;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
int n, x;
std::cin >> n;
std::vector<int> a(n + 1), c(n + 1);
for (int i = 1; i <= n; i++)
{
std::cin >> x;
if (x <= n)
a[x]++;
}
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j += i)
c[j] += a[i];
std::cout << *std::max_element(c.begin(), c.end()) << '\n';
}
}
#include
typedef std::pair<int, int> pii;
typedef long long LL;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
auto cal = [](std::vector<int> vec) -> LL
{
std::sort(vec.begin(), vec.end());
int n = (int)vec.size();
LL res = 0;
for (int j = 0; j < n;)
{
int p = j;
while (++j < n && vec[j] == vec[j - 1])
;
LL len = j - p;
res += len * (len - 1);
}
return res;
};
int n;
std::cin >> n;
std::vector<pii> points(n);
for (auto &point : points)
std::cin >> point.first >> point.second;
std::vector<int> x, y, xplusy, ysubx;
std::transform(points.begin(), points.end(), std::back_inserter(x), [](const pii &point)
{ return point.first; });
std::transform(points.begin(), points.end(), std::back_inserter(y), [](const pii &point)
{ return point.second; });
std::transform(points.begin(), points.end(), std::back_inserter(xplusy), [](const pii &point)
{ return point.first + point.second; });
std::transform(points.begin(), points.end(), std::back_inserter(ysubx), [](const pii &point)
{ return point.second - point.first; });
LL res = cal(x) + cal(y) + cal(xplusy) + cal(ysubx);
std::cout << res << '\n';
}
}
已知 n 个横坐标轴上点 a_i,和 m 个约束
每个约束 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UWSk2lAa-1690212116814)(https://cdn.nlark.com/yuque/__latex/6dc3974d55cff8726ef9c61a7a825894.svg#card=math&code=a_x%20-%20a_y%20%3D%20d&id=KU9og)], 问是否存在 n 个点满足所有约束
#include
typedef std::pair<int, int> pii;
typedef long long LL;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--)
{
int n, m;
std::cin >> n >> m;
std::vector<std::vector<pii>> E(n + 1);
for (int i = 1; i <= m; i++)
{
int u, v, d;
std::cin >> u >> v >> d;
E[u].push_back(std::make_pair(v, d));
E[v].push_back(std::make_pair(u, -d));
}
bool flag = true;
std::vector<bool> vis(n + 1);
std::vector<LL> dis(n + 1);
std::function<void(int, LL)> dfs;
dfs = [&](int now, LL w)
{
vis[now] = true;
dis[now] = w;
for (auto item : E[now])
{
int &v = item.first;
int &d = item.second;
if (!vis[v])
dfs(v, w + d);
else
{
if (dis[v] - dis[now] != d)
flag = false;
}
}
};
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i, 0);
std::cout << (flag ? "YES\n" : "NO\n");
}
}