A - Goals of Victory
题目描述
总共有n
个球队,两两进行比赛,每个队每场比赛的得分为当场比赛总进球数减去对手在当场比赛中的总进球数,现在给出前n-1
个队伍每个队伍的最终得分,请你求出第n
个球队的得分。
思路:模拟
- 用
1
和剩余的得分值直接模拟每个球队得分的得与失,直至将每个队的得分模拟至0
。
public static void solve() throws IOException {
int n = readInt();
int[] ef = new int[n + 10];
for (int i = 1; i <= n - 1; i++) {
ef[i] = readInt();
}
int[][] match = new int[n + 10][n + 10];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
match[i][j] = inf;
}
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n; j++) {
if (match[i][j] == inf && i != j) {
if (ef[i] > 0) {
if (j != n) {
match[i][j] = 1;
match[j][i] = -1;
ef[j]++;
ef[i]--;
} else {
match[i][j] = ef[i];
match[j][i] = -ef[i];
ef[j] += ef[i];
ef[i] = 0;
}
} else if (ef[i] < 0) {
if (j != n) {
match[i][j] = -1;
match[j][i] = 1;
ef[j]--; ef[i]++;
} else {
match[i][j] = ef[i];
match[j][i] = -ef[i];
ef[j] -= ef[i]; ef[i] = 0;
}
} else {
match[i][j] = 0;
match[j][i] = 0;
}
}
}
}
int sum = 0;
for (int i = 1; i <= n - 1; i++) {
sum += match[n][i];
}
printWriter.println(sum);
}
B - Helmets in Night Light
题目描述
你是村长,可以直接将公告发给一位或多位居民,发给每人需要花费 p p p。已知公告的第 i i i个居民可以将公告再发给 a i a_i ai个居民,发给每人需要花费 b i b_i bi,现请你求出如果要让 n n n个居民都知道公告,最低费用为多少。
思路:排序
- 不断将 b i b_i bi值较小的入队,如果 b i b_i bi大于了 p p p,那么花费肯定更高,直接
break
。
public static void solve() throws IOException{
int n = readInt(), p = readInt();
Pair[] pairs = new Pair[n + 10];
for (int i = 1; i <= n; i++) {
pairs[i] = new Pair();
pairs[i].first = readInt();
}
for (int i = 1; i <= n; i++) {
pairs[i].second = readInt();
}
long total = 1l * n * p;
Arrays.sort(pairs, 1, n + 1, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
if (o1.second == o2.second) {
return o2.first - o1.first;
}
return o1.second - o2.second;
}
});
Deque<Pair> deque = new LinkedList<>();
deque.offer(pairs[1]);
int cur = 1;
while (deque.size() > 0) {
Pair pair = deque.poll();
if (pair.second < p) {
int t = cur + pair.first;
for (int i = cur + 1; i <= t && i <= n; i++) {
total -= (p - pair.second);
deque.offer(pairs[i]);
cur = i;
}
} else {
break;
}
}
printWriter.println(total);
}
C - Joyboard
题目描述
一个屏幕有 n + 1 n + 1 n+1个插槽,你需要在第 n + 1 n + 1 n+1和插槽的位置填入一个 0 ∼ m 0 \sim m 0∼m的数,使得屏幕上所有插槽总共正好显示 k k k个不同的数,屏幕上 1 ∼ n 1 \sim n 1∼n位置上的插槽上显示的数为 a i = a i + 1 m o d i a_i = a_{i + 1} mod \ i ai=ai+1mod i,请你求出在插槽 n + 1 n + 1 n+1的位置上有多少种方案可以满足上述条件。
思路:分类讨论
- 假设在 n + 1 n+1 n+1的位置插入元素为 x x x
- x = 0 x = 0 x=0, 只有一种数字
- x > 0 & & x ≤ n x \gt 0 \ \&\& \ x \leq n x>0 && x≤n时,只有 0 0 0和 x x x这两种数字
- x > n x \gt n x>n时,如果 x x x是 n n n的倍数,只有 0 0 0和 x x x这两种数字,否则总共有 3 3 3种数字。
public static void solve() throws IOException{
int n = readInt(), m = readInt(), k = readInt();
if (k == 1) {
printWriter.println(m >= 0 ? 1 : 0);
} else if (k == 2) {
if (m == 0) {
printWriter.println(0);
} else if (m <= n) {
printWriter.println(m);
} else {
printWriter.println(n - 1 + m / n);
}
} else if (k == 3) {
if (m >= n) {
printWriter.println(m - n - m / n + 1);
} else {
printWriter.println(0);
}
} else {
printWriter.println(0);
}
}