暑假-S组-第四场

· · 个人记录

概况

考出中位数了 666

传送门

题目编号 T1 T2 T3
题目难度 \color{orange}黄 \color{green}绿 \color{green}绿
考察知识点 数学规律 乘法原理 二分

分析

T1

Wrong Cause

NoThing

Solution

既然过了就简单一点吧

根据题目推出式子:

(n+2)×(n)×(n+1)/2

取模并输出即可。但注意 "/2" 后取模否则 \color{orange}{50}pts

代码不贴

T2

假题一道。易用乘法分配律得到:不管你先合并哪两杯,答案都一样。而且每个值其实都只会和另外一个值搭配一次。又容易发现前面的选取的总和 × 下一杯,下一杯的大小不会对原来的答案造成影响,所以存在单调性,可以按照原来的输入顺序统计。

所以有:

int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i - 1; j++)
sum += (a[i] * a[j]);

优化得:

int sum = 0, tmp = 0;
for (int i = 1; i <= n; i++)
{
sum += (tmp * a[i]);
tmp += a[i];
}

再来想想第二问,既然打乱排列顺序没事,那就最终的结果计算应该是按照 首先从 n 个数字之中选取两个数字 然后从剩余的 n−1 个数字之中选取两个数字 然后从剩余的 n−2 个数字之中选取两个数字 ... 最终到剩余的两个数字之中选取两个数字。在根据乘法原理完事

int ans = 1;
for (int i = 2; i <= n; i++)
{
int x = x * (x - 1) / 2; // i 个里面选 2 个的方案数
ans *= x;
}

完整实现:


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e4 + 10;
const int mod = 1e9 + 7;
int t, n, op, a[N];
int E(int x)
{
return (x - 1) * x / 2;
}
signed main()
{
//freopen("T2.in", "r", stdin);
//freopen("T2.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> t;
while (t--)
{
cin >> n >> op;
for (int i = 1; i <= n; i++) cin >> a[i];
int tmp = 0, ans = 0, sum = 1;
for (int i = 1; i <= n; i++)
{
ans += tmp * a[i];
tmp += a[i];
tmp %= mod;
ans %= mod;
}
cout << ans << " ";
for (int i = 2; i <= n; i++)
{
int x = E(i);
x %= mod;
sum *= x;
sum %= mod;
}
if (op == 0) cout << "0\n";
else cout << sum << "\n";
}
return 0;
}
## T3
### Wrong Cause
没有康建单调性,又没有时间了。于是~~理所当然的~~拿下 $\color{red}{20}$ 分的特殊数据。

### Solution
只讲 $\color{green}{100}pts$ 做法。

> 发现:如果数据满足能打包 $n$ 盒,自然也能打包 $n - 1$ 盒,具有单调性
>
> 考虑二分。二分一个 $x$ 表示要打包多少包。然后 ```check``` 一下。每一种**最多**取 $x$ 个,验证能否打出 $x$ 包即可。
>
```cpp
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5e5 + 7;
int a[maxn], n, k;
bool check(int x)
{
    int sum = 0;
    for (int i = 1; i <= n; i++) sum += min(a[i], x);
    return sum >= x * k;
}
signed main()
{
    int l = -1, r = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        r += a[i];
    }
    r /= k;
    r++;
    while (l + 1 < r)
    {
        int mid = (l + r) / 2;
        if (check(mid)) l = mid;
        else r = mid;
    }
    cout << l;
    return 0;
}