Codeforces Round 861 (Div. 2)(A~C)题解

news/2025/2/21 8:41:50/文章来源:https://www.cnblogs.com/cjcf/p/18724221

比赛链接: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;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/886718.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

frame/tab 切换,一些技巧

点击这里,边看视频讲解,边学习以下内容 frame切换 请大家点击这里,打开这个链接 如果我们要 选择 下图方框中 所有的 蔬菜,使用css选择,怎么写表达式? 当然,要先查看到它们的html元素特征大家可能会照旧写出如下代码:from playwright.sync_api import sync_playwrightp…

CSS选择器 定位方法

定位元素的重要性 前面这段代码from playwright.sync_api import sync_playwrightp = sync_playwright().start() browser = p.chromium.launch(headless=False) page = browser.new_page() page.goto("https://www.byhy.net/cdn2/files/selenium/stock1.html")# 输入…

Arduino-Esp8266 OTA升级

#include <ESP8266WiFi.h> #include <ESP8266httpUpdate.h> #include <Ticker.h>/******需要修改的地方****************/#define wifi_name "GT-2G" //WIFI名称,区分大小写,不要写错 #define wifi_password "gt#@10000" …

【IDEA】idea接入AutoDev插件并配置DeepSeek

1. AutoDev Quick Start https://ide.unitmesh.cc/quick-start2. AutoDev 下载 https://github.com/unit-mesh/auto-dev/releases3. 根据IDEA版本进行下载 比如我的IDEA版本是2024.3.2.1 那么我应该下载的就是241.zip 需要根据最新的quick-start去release下载对应版本 4. IDEA…

CF559E Gerald and Path 题解

CF559E Gerald and Path 很困难的 DP 题,状态不是很好想。对于这种线段覆盖类题目,显然先覆盖哪个线段没有影响,我们可以通过按照端点位置升序排序后按照顺序考虑,这样可能会有一些额外性质。 之后,考虑转移时需要什么东西来刻画一个状态的轮廓。显然我们需要知道现在是第…

中国PostgreSQL数据库认证体系和学习方向

中国PostgreSQL数据库认证体系和学习方向PostgreSQL认证,指的是PostgreSQL数据库管理员的能力认证,用来判断从业人员是否具备管理和维护PostgreSQL数据库的能力,由于数据库中存放着很多重要的数据,所以对于从业人员的要求极高,所以企业在招聘相关岗位的时候,会优先考虑有…

ATTCK实战系列(一)

环境下载 下载靶场环境,并导入虚拟机分别是win2003、win7、winserver2008配置网络 虚拟机——编辑——虚拟机网络编辑器——添加网络VMnet2——仅主机模式分配的地址是192.168.52.0配置好地址后,需要将win2003和winserver2008的网卡设置为VMnet2win7的网络需要两张,一张为VM…

DHTMLX Gantt 甘特图导出全数据图/PDF

最近有个需求,将项目甘特图导出图片,但发现问题:当项目甘特图内的行数很多时(这是必然,当项目周期长,建立的任务很多,就会出现很多任务行),超过了甘特图的可视区域,就会出现滚动条,导出图片的时候只会导出可视区域内显示的甘特图,其他非可视区域的内容没导出(就是…

185 注意力模型

我们利用\(\text{GRU}\)或者\(\text{LSTM}\)构建一个双向循环神经网络如下然后预测的时候我们使用普通的RNN,但是这个RNN的输入取决于源句子的每一个单词的加权和。我们用\(y\)表示预测的句子,\(a\)表示输入的句子,那么也就是说我们定义\(\alpha^{\left<t,t^{}\right>…

生产问题系统卡顿-涉及io磁盘性能排查

1.问题背景: 用户反馈系统卡顿,用top命令排查后服务器资源正常,并且数据库无阻塞和锁表。 2.解决问题方法: 用iostat命令对磁盘性能进行排查,发现io性能很差3.iostat命令详细解析 需要更详细看到磁盘指标情况需要用到iostat命令进行分析。 iostat -x -m 2命令说明:-x 显示…