[AGC049E] Increment Decrement
Genius_Star · · 题解
模拟赛题,场上只会
思路:
首先考虑
考虑区间操作和手动互相之间没有影响,可以考虑先钦定做了区间操作,最后手动来补;于是设通过区间操作使得
于是容易想到
因为值域特别大,感觉不是很能优化的方式;但是你发现绝对值涉及很多,你容易归纳出
初始
然后加上
考虑用一个可重集
-
若
i = 1 :不存在斜率为-1 和c + 1 的段,直接插入两个a_i 。 -
否则,必然上次插入
a_{i - 1} 后存在斜率为-1 和c + 1 的段,于是删除最左边和最右边的拐点,即推平操作;然后再插入两个a_i 。
显然答案一定是斜率
现在你就可以
考虑扩展一下,你发现上面最后的答案被转化成了很优美的形式,显然可以拆贡献,一个
- 即在上述维护可重集的过程中
p 作为最小值的方案数。
看起来比较困难,考虑差分一下,用
不太好求,考虑容斥用总的
于是此时考虑
其中
总时间复杂是
完整代码:
#include<bits/stdc++.h>
#define lowbit(x) x & (-x)
#define pi pair<ll, ll>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int N = 105, mod = 1e9 + 7;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
//int n, m, c, ans;
//int a[N];
//multiset<int> S;
//int main(){
// n = read(), m = read(), c = read();
// for(int i = 1; i <= n; ++i){
// a[i] = read();
// ans = (ans + a[i]) % mod;
// }
// if(c > n){
// write(ans);
// return 0;
// }
// ans = 0;
// while(c--)
// S.insert(0);
// for(int i = 1; i <= n; ++i){
// if(i > 1){
// S.erase(S.begin());
// S.erase(--S.end());
// }
// S.insert(a[i]);
// S.insert(a[i]);
// ans = ((ans + a[i]) % mod - *S.begin() + mod) % mod;
// }
// write(ans);
// return 0;
//}
int n, m, c, ans, cnt;
int a[N][N], pw[N], p[N], h[N * N];
int dp[N][N];
inline int qpow(int a, int b){
int ans = 1;
while(b){
if(b & 1)
ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return ans;
}
inline int calc(int mid){
for(int i = 1; i <= n; ++i){
p[i] = lower_bound(a[i] + 1, a[i] + m + 1, mid) - (a[i] + 1);
// cerr << "i: " << p[i] << '\n';
}
memset(dp, 0, sizeof(dp));
for(int j = 1; j <= m; ++j)
++dp[1][c * (0 >= mid) + 2 * (a[1][j] >= mid)];
for(int i = 1; i < n; ++i){
for(int j = 0; j <= c + 2; ++j){
if(!dp[i][j])
continue;
// cerr << "value: " << i << ' ' << j << ' ' << dp[i][j] << '\n';
int nj = j - (j > 0) - (j == c + 2);
// cerr << "nxt: " << nj << '\n';
dp[i + 1][nj] = (dp[i + 1][nj] + 1ll * p[i + 1] * dp[i][j] % mod) % mod;
dp[i + 1][nj + 2] = (dp[i + 1][nj + 2] + 1ll * (m - p[i + 1]) * dp[i][j] % mod) % mod;
}
}
int sum = 1ll * n * pw[n] % mod;
for(int i = 1; i <= n; ++i){
sum = (sum - 1ll * dp[i][c + 2] * pw[n - i] % mod + mod) % mod;
// if(dp[i][c + 2])
// cerr << "point: " << i << '\n';
}
return sum;
}
int main(){
n = read(), m = read(), c = read();
pw[0] = 1;
for(int i = 1; i <= n; ++i){
pw[i] = 1ll * pw[i - 1] * m % mod;
for(int j = 1; j <= m; ++j){
a[i][j] = read();
h[++cnt] = a[i][j];
ans = (ans + a[i][j]) % mod;
}
sort(a[i] + 1, a[i] + m + 1);
}
if(c > n){
write(1ll * ans * qpow(m, mod - 2) % mod);
putchar('\n');
return 0;
}
ans = 1ll * pw[n - 1] * ans % mod;
sort(h + 1, h + cnt + 1);
cnt = unique(h + 1, h + cnt + 1) - (h + 1);
// cerr << calc(4) << '\n';
// cerr << "sum: " << ans << '\n';
for(int i = 1; i <= cnt; ++i){
ans = (ans - 1ll * (calc(h[i] + 1) - calc(h[i]) + mod) % mod * h[i] % mod + mod) % mod;
// cerr << h[i] << ' ' << 1ll * (calc(h[i] + 1) - calc(h[i]) + mod) % mod << ' ' << calc(h[i] + 1) << ' ' << calc(h[i]) << '\n';
}
// cerr << 500000014ll * pw[n] % mod << '\n';
write(1ll * ans * qpow(pw[n], mod - 2) % mod);
return 0;
}