比赛链接:https://codeforces.com/contest/1808
A. Lucky Numbers
思路:这道题如果想去直接构造挺难的,但是我们可以很简单想到0和9在距离一个数的上下1000范围内是一定会出现的,所以就直接枚举即可
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<functional>
#include<stack>
#include<string>
#define ll long long
#define lowbit(x) (x & -x)
//#define endl "\n"// 交互题记得删除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
//const ll p=rnd()%mod;
const ll N = 205000;
#define F first
#define S second
ll ksm(ll x, ll y)
{ll ans = 1;while (y){if (y & 1){ans = ans % mod * (x % mod) % mod;}x = x % mod * (x % mod) % mod;y >>= 1;}return ans % mod % mod;
}
ll gcd(ll x, ll y)
{if (y == 0)return x;elsereturn gcd(y, x % y);
}
void fio()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}
struct s
{ll l, r;bool operator<(const s& a){return r < a.r;}
};
int main()
{fio();ll t;cin >> t;while (t--){ll l,r;cin>>l>>r;ll pd=0;// while(1)// {// }ll ans=-5;ll cnt;for(ll i=l;i<=min(l+1000,r);i++){ll u=i;ll minn=0,maxx=1e18;while(u){minn=max(minn,u%10);maxx=min(maxx,u%10);u/=10;}if(ans<abs(maxx-minn))cnt=i,ans=abs(maxx-minn);//ans=max(ans,abs(maxx-minn));}for(ll i=r;i>=max(l,r-1000);i--){ll u=i;ll minn=0,maxx=1e18;while(u){minn=max(minn,u%10);maxx=min(maxx,u%10);u/=10;}if(ans<abs(maxx-minn))cnt=i,ans=abs(maxx-minn);}cout<<cnt<<endl;}
}
B. Playing in a Casino
思路:答案是每列操作之和,那我们就对每一列单独计算,可以想到排个序,然后从大往小进行叠加就可以了。
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<functional>
#include<stack>
#include<string>
#define ll long long
#define lowbit(x) (x & -x)
//#define endl "\n"// 交互题记得删除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
//const ll p=rnd()%mod;
const ll N = 205000;
#define F first
#define S second
ll ksm(ll x, ll y)
{ll ans = 1;while (y){if (y & 1){ans = ans % mod * (x % mod) % mod;}x = x % mod * (x % mod) % mod;y >>= 1;}return ans % mod % mod;
}
ll gcd(ll x, ll y)
{if (y == 0)return x;elsereturn gcd(y, x % y);
}
void fio()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}
struct s
{ll l, r;bool operator<(const s& a){return r < a.r;}
};
//ll a[450000];
int main()
{fio();ll t;cin >> t;while (t--){ll n,m;cin>>n>>m;vector<ll>a[m+5];for(ll i=1;i<=n;i++){for(ll j=1;j<=m;j++){ll x;cin>>x;a[j].push_back(x);}}for(ll j=1;j<=m;j++)sort(a[j].begin(),a[j].end());ll ans=0;for(ll j=1;j<=m;j++){ll cnt=0;ll f=ans;ll cs=0;for(ll k=(ll)a[j].size()-1;k>=0;k--){ll u=a[j][k];if(cnt>0)ans+=cnt-u*cs;cnt+=u;cs++;}//cout<<ans-f<<endl;}cout<<ans<<endl;}
}
C. Unlucky Numbers
思路:可以想到如果左右数字的长度不一样,答案就是999...,不一样,可以dfs暴力,用标记表示是否得被上限或者下限;其实还有更简单的方法,直接找到第一个不想等的区域,然后暴力下这个区间,然后再暴力下后面的区间(后面的默认一样即可)最坏情况,9*9.这种好写多了
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<deque>
#include<cctype>
#include<string.h>
#include<math.h>
#include<time.h>
#include<random>
#include<functional>
#include<stack>
#include<string>
#define ll long long
#define lowbit(x) (x & -x)
//#define endl "\n"// 交互题记得删除
using namespace std;
mt19937 rnd(time(0));
const ll mod = 998244353;
//const ll p=rnd()%mod;
const ll N = 205000;
#define F first
#define S second
ll ksm(ll x, ll y)
{ll ans = 1;while (y){if (y & 1){ans = ans % mod * (x % mod) % mod;}x = x % mod * (x % mod) % mod;y >>= 1;}return ans % mod % mod;
}
ll gcd(ll x, ll y)
{if (y == 0)return x;elsereturn gcd(y, x % y);
}
void fio()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
}
struct s
{ll l, r;bool operator<(const s& a){return r < a.r;}
};
//ll a[450000];
int main()
{fio();ll t;cin >> t;while (t--){string l, r;cin >> l >> r;ll len = max(l.size(), r.size());if (l.size() != r.size()){for (ll i = 0; i < l.size(); i++)cout << "9";cout << endl;continue;}ll fg[250], an, e = 1e18;function<void(ll, ll, ll, ll)>dfs = [&](ll x, ll y, ll z, ll k){ll ans = 1e18;if ((x && y) || k == len){if (k != len) fg[k] = z, dfs(x, y, z, k + 1);else{ll jk = 0;for (ll i = 0; i < k; i++){jk = jk * 10 + fg[i];}ll maxx = 0, minn = 1e18;ll pd = 0;for (ll i = 0; i < k; i++){if (fg[i] > 0)pd = 1;if (pd == 0)continue;minn = min(minn, fg[i]);maxx = max(maxx, fg[i]);}//if (jk == 3343)//cout << z << endl;ans = maxx - minn;if (e > ans)e = ans, an = jk;}return;}else if (x){ll u = r[k] - '0';if (u == z)fg[k] = u, dfs(1, 0, z, k + 1);else if (u > z) fg[k] = z, dfs(1, 1, z, k + 1);else{fg[k] = u, dfs(1, 0, z, k + 1);if (u != 0)fg[k] = u - 1, dfs(1, 1, z, k + 1);}}else if (y){ll u = l[k] - '0';if (u == z) fg[k] = u, dfs(0, 1, z, k + 1);else if (u > z){fg[k] = u, dfs(0, 1, u, k + 1);if (u != 9)fg[k] = u + 1, dfs(1, 1, z, k + 1);}else fg[k] = z, dfs(1, 1, z, k + 1);}else{ll k1 = l[k] - '0';ll k2 = r[k] - '0';if (k1 == k2)fg[k] = k1, dfs(0, 0, z, k + 1);else if (z == k1){fg[k] = z, dfs(0, 1, z, k + 1);if (z + 1 != k2)fg[k] = z + 1, dfs(1, 1, z, k + 1);else fg[k] = z + 1, dfs(1, 0, z, k + 1);}else if (z == k2){fg[k] = z, dfs(1, 0, z, k + 1);if (z - 1 != k1)fg[k] = z - 1, dfs(1, 1, z, k + 1);else fg[k] = z - 1, dfs(0,1, z, k + 1);}else if (z < k1){fg[k] = k1, dfs(0, 1, z, k + 1);if (k1 + 1 != k2)fg[k] = k1 + 1, dfs(1, 1, z, k + 1);else fg[k] = k1 + 1, dfs(1, 0, z, k + 1);}else if (z > k2){fg[k] = k2, dfs(1, 0, z, k + 1);if (k2 - 1 != k1)fg[k] = k2 - 1, dfs(1, 1, z, k + 1);else fg[k] = k2 - 1, dfs(0, 1, z, k + 1);}else fg[k] = z, dfs(1, 1, z, k + 1);}return;};for (ll i = 0; i <= 9; i++){dfs(0, 0, i, 0);}cout << an << endl;}
}