题目1.
给出一个数组a,需要对其做k次操作,每次操作是让其中的一个元素的值+1,k次操作之后使得数组的最大值最小。
#include <stdio.h>
int main()
{
const int N = 100000 + 5;
long long a[N];
int n;
long long k;
long long maxn = 0;
scanf("%d %lld", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
maxn = maxn > a[i] ? maxn : a[i];
}
long long sum = k;
for (int i = 0; i < n; ++i) {
long long x = maxn - a[i];
sum -= x;
if (sum <= 0) {
break;
}
}
if (sum > 0) {
long long ans = (maxn + sum/n);
long long rest = sum%n;
if (rest > 0) ans++;
printf("%lld\n", ans);
}
else {
printf("%lld\n", maxn);
}
return 0;
}
题目2.
(笔试过程中并没有做出来,但是样例过了)
给出一个n*m的地图还给出k支部队,地图上1表示沼泽 不能走,0表示道路 能走,每支部队过1单位的时间能走1格,问这k支部队最快多久能汇合,如果不能汇合则输出-1。
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string.h>
using namespace std;
struct node {
int x, y;
int cost;
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int vis[505][505] = {0};
int tmpVis[105][105];
int maps[505][505];
int main()
{
int res = 0x3f3f3f3f;
memset(tmpVis, 0x3f3f3f3f, sizeof(tmpVis));
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j)
scanf("%d", &maps[i][j]);
}
int k;
scanf("%d", &k);
while (k--) {
int x, y;
scanf("%d%d", &x, &y);
queue<node> q;
node first = node{x, y, 0};
q.push(first);
memset(tmpVis, 0x3f3f3f3f, sizeof(tmpVis));
tmpVis[x][y] = 0;
while (!q.empty()) {
node head = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int tmpx = head.x + dx[i];
int tmpy = head.y + dy[i];
if (tmpx <= 0 || tmpx > n || tmpy <= 0 || tmpy > m)
continue;
int tmpCost = head.cost + 1;
if (maps[tmpx][tmpy] == 0 && tmpCost < tmpVis[tmpx][tmpy]) {
tmpVis[tmpx][tmpy] = tmpCost;
node tmp = node{tmpx, tmpy, tmpCost};
q.push(tmp);
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
vis[i][j] = max(vis[i][j], tmpVis[i][j]);
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
res = min(res, vis[i][j]);
}
}
if (res == 0x3f3f3f3f) res = -1;
printf("%d\n", res);
return 0;
}
|