这个题目直接暴力,还是有点难想,我没有想出来,有点思维。
#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 ", 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 ", ans); } } printf("%lld %lld %lld ", x - 1, y - 1, z - 1); return 0; }