A Lucky Numbers
题目大意:
解题思路:
如果r-l>100,那么肯定存在一个数含有90,找出即可;
如果r-l<100,就直接暴力枚举
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
ll T, l, r;
int f(int x) {
int ma = 0, mi = 10;
while (x) {
int y = x % 10;
ma = max(ma, y);
mi = min( mi, y);
x /= 10;
}
return ma - mi;
}
int ans;
void slove() {
cin >> l >> r;
if (r - l >= 100) {
for (int i = (r / 100 + 1) * 100 + 90; i >= l; i -= 100) {
if (i <= r) {
cout << i << endl;
break;
}
}
} else {
int ma = -1;
for (int i = l; i <= r; i++) {
if (ma < f(i)) {
ma = f(i);
ans = i;
}
}
cout << ans << endl;
}
}
int main() {
cin >> T;
while (T--) slove();
return 0;
}
B Playing in a Casino
题目大意:
解题思路:
先对每一列排序,再求每一行的前缀和 ,那么
(自己推出来的)
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e5 + 5;
ll T, n, m;
ll x;
bool cmp(int a, int b) {
return a > b;
}
ll s[N] = {};
vector<int>g[N];
void solve() {
cin >> n >> m;
memset(s, 0, sizeof(s));
for (int i = 1; i <= m; i++) g[i].clear();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> x;
g[j].push_back(x);
}
}
for (int i = 1; i <= m; i++) {
sort(g[i].begin(), g[i].end(), cmp);
}
for (int i = 0; i < n; i++) {
for (int j = 1; j <= m; j++) {
s[i + 1] += g[j][i];
}
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += (n + 1 - 2 * i) * s[i];
}
cout << ans << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}