B. Math Show 暴力 C - Four Segments
2024-09-07 13:36:03
这个题目直接暴力,还是有点难想,我没有想出来,有点思维。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <list>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
int a[110], vis[110];
int sum[110];
int main()
{
int n, k, m, ans = 0, sum = 0;
scanf("%d%d%d", &n, &k, &m);
for (int i = 1; i <= k; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
sort(a + 1, a + 1 + k);
for (int i = 0; i <= n; i++) {
int res = m - i * sum;
if (res < 0) break;
int num = (k + 1)*i;
for (int j = 1; j <= k; j++) {
if (res >= a[j] * (n - i)) {
res -= a[j] * (n - i);
num += n - i;
}
else {
num += res / a[j];
break;
}
}
ans = max(ans, num);
}
printf("%d\n", ans);
return 0;
}
这个题目我觉得还是有点难,这个题目求 res= sum[1,x-1] - sum[x,y-1] + sum[y,z-1] -sum[z, n] 的最大值
暴力枚举中间的y,然后两边贪心求x , z
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <list>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 5e3 + 10;
ll sum[maxn], a[maxn]; int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), sum[i] = sum[i - 1] + a[i];
ll ans = 0, x = 1, y = 1, z = 1;
for(int i=1;i<=n;i++)
{
ll res1 = 0, mark1 = 1;
for (int j = i; j <= n + 1; j++) {
ll num = sum[j - 1] * 2 - sum[i - 1] - sum[n];
if (num > res1) res1 = num, mark1 = j;
}
ll res2 = 0, mark2 = 1;
for (int j = 1; j <= i; j++) {
ll num = 2 * sum[j - 1] - sum[i - 1];
if (num > res2) res2 = num, mark2 = j;
}
if(res1+res2>ans)
{
ans = res1 + res2;
x = mark2, y = i, z = mark1;
// printf("ans=%d\n", ans);
}
}
printf("%lld %lld %lld\n", x - 1, y - 1, z - 1);
return 0;
}
最新文章
- [PCL]点云渐进形态学滤波
- JVM学习笔记:虚拟机的类加载机制
- 《Java中的包机制》
- 最短路(Bellman_Ford) POJ 3259 Wormholes
- easyui tab 关闭
- ggplot2 demo
- 隐藏NGINX服务器名称 和版本号
- 如何使用wait(), notify() and notifyAll() – Java
- BZOJ 1072: [SCOI2007]排列perm [DP 状压 排列组合]
- UVa-156 Ananagrams(map映射)
- java线程和多线程同步
- Python 两个星号(**)的 参数
- CCF-URL映射-(正则匹配)-20180303
- CentOS75 安装Oracle18c
- Linux &#39;XXXXXX&#39; ";is not in the sudoers file. This incident will be reported"; 解决方法
- 嵌入式Linux软件工程师面试题一
- ELK基础学习
- Python 正则:前后界定和前后非界定
- leetcode 之Search in Rotated Sorted Array(三)
- 在父页面和其iframe之间函数回调 父页面回调iframe里写的函数