题目链接:P2671 [NOIP2015 普及组] 求和 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
#define mod 10007
using namespace std;
int num[100010], color[100010], s[100010][2], sum[100010][2];
int main() {
int n, m, ans = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> num[i];
}
for (int i = 1; i <= n; i++) {
cin >> color[i];
s[color[i]][i % 2]++;
sum[color[i]][i % 2] = (sum[color[i]][i % 2] + i) % mod;
}
for (int i = 1; i <= n; i++) {
ans = (ans + num[i] * (i * (s[color[i]][i % 2] - 2) % mod + sum[color[i]][i % 2]) % mod) % mod;
}
cout << ans;
return 0;
}
题目链接:P1115 最大子段和 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
#include
using namespace std;
int a[200010], sum[200010];
int main() {
int n, ans = -(1 << 29);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (i == 0) {
sum[i] = a[i];
} else {
sum[i] = max(a[i], sum[i - 1] + a[i]);
}
ans = max(ans, sum[i]);
}
cout << ans;
return 0;
}
题目链接:P3397 地毯 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
using namespace std;
int a[1005][1005];
int main() {
int n, m, x1, y1, x2, y2;
cin >> n >> m;
while (m--) {
cin >> x1 >> y1 >> x2 >> y2;
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
a[i][j]++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
题目链接:P3406 海底高铁 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
#include
using namespace std;
long long p[100010], c[100010][3], a[100010], f[100010];
int main() {
unsigned long long n, m, ans = 0;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> p[i];
}
for (int i = 1; i <= n - 1; i++) {
cin >> c[i][0] >> c[i][1] >> c[i][2];
}
for (int i = 0; i < m - 1; i++) {
if (p[i] < p[i + 1]) {
a[p[i]]++;
a[p[i + 1]]--;
} else {
a[p[i + 1]]++;
a[p[i]]--;
}
}
for (int i = 1; i <= n - 1; i++) {
if (i == 1) {
f[i] = a[i];
} else {
f[i] = f[i - 1] + a[i];
}
ans += min(f[i] * c[i][0], c[i][2] + f[i] * c[i][1]);
}
cout << ans;
return 0;
}
题目链接:P1719 最大加权矩形 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
#include
#include
using namespace std;
int a[150][150], t[150], dp[150];
int main() {
int n, ans = -(1 << 29);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
memset(t, 0, sizeof(t));
for (int j = i; j <= n; j++) {
for (int k = 1; k <= n; k++) {
t[k] += a[j][k];
}
memset(dp, 0, sizeof(dp));
for (int r = 1; r <= n; r++) {
dp[r] = max(t[r], dp[r - 1] + t[r]);
ans = max(ans, dp[r]);
}
}
}
cout << ans;
return 0;
}
题目链接:P2004 领地选择 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
#include
using namespace std;
int a[1010][1010], f[1010][1010];
int main() {
int n, m, c, x, y, maxx = -(1 << 29);
cin >> n >> m >> c;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
f[i][j] = a[i][j] + f[i][j - 1] + f[i - 1][j] - f[i - 1][j - 1];
}
}
for (int i = c; i <= n; i++) {
for (int j = c; j <= m; j++) {
if (f[i][j] - f[i][j - c] - f[i - c][j] + f[i - c][j - c] > maxx) {
maxx = f[i][j] - f[i][j - c] - f[i - c][j] + f[i - c][j - c];
x = i - c + 1;
y = j - c + 1;
}
}
}
cout << x << ' ' << y;
return 0;
}
题目链接:P5638 【CSGRound2】光骓者的荣耀 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include
#include
using namespace std;
long long a[1000010], f[1000010];
int main() {
long long n, k, maxx = 0;
cin >> n >> k;
for (int i = 1; i <= n - 1; i++) {
cin >> a[i];
f[i] = f[i - 1] + a[i];
}
for (int i = 0; i + k <= n - 1; i++) {
if (f[i + k] - f[i] > maxx) {
maxx = f[i + k] - f[i];
}
}
cout << f[n - 1] - maxx;
return 0;
}