2024牛客寒假算法基础集训营2 题解 ( A,B,E,F,I,J )
2024牛客寒假算法基础集训营2
A Tokitsukaze and Bracelet
题意
根据所给信息,求出强化等级
思路
每个数字分别对应不同的权值,直接判断计数即可
代码
/*******************************
| Author: AlwaysBeShine
| Problem: Tokitsukaze and Bracelet
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67742/A
| When: 2024-02-05 13:03:28
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
unordered_map<int,int>qh;
qh[32] = qh[31] = qh[30] = qh[29] = 0;
qh[34] = qh[36] = qh[38] = qh[40] = 1;
qh[45] = 2;
qh[100] = 0;
qh[150] = 1;
qh[200] = 2;
int n;
cin >> n;
for(int i = 0;i < n;i++){
int sum = 0;
int a,b,c;
cin >> a >> b >> c;
cout << qh[a] + qh[b] + qh[c] << endl;
}
return 0;
}
B Tokitsukaze and Cats
题意
有
思路
两只相邻格子的猫,可以在他们联通的那部分少放一片防猫片
即每次读入时判断与当前格子相邻的
代码
/*******************************
| Author: AlwaysBeShine
| Problem: Tokitsukaze and Cats
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67742/B
| When: 2024-02-05 13:09:09
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool cat[303][303];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n,m,k,sum = 0;
cin >> n >> m >> k;
sum = 4*k;
for(int i = 1;i <= k;i++){
ll x,y;
cin >> x >> y;
cat[x][y] = true;
if(cat[x-1][y])sum--;
if(cat[x+1][y])sum--;
if(cat[x][y-1])sum--;
if(cat[x][y+1])sum--;
}
cout << sum << endl;
return 0;
}
E Tokitsukaze and Eliminate (easy)
题意
有
思路
用 set 存储剩余的颜色,每个颜色对应一个 大根堆 ,堆顶元素代表着对应颜色最右侧的宝石的位置,每次取堆顶元素时都要检查当前元素是否合法(是否该位置的宝石已经在之前的操作过程中被清除),不合法则弹出。
如果某一个颜色对应的堆顶已空,则从 set 容器中将对应颜色删除,直至 set 容器为空,即为删除完毕。
代码
/*******************************
| Author: AlwaysBeShine
| Problem: Tokitsukaze and Eliminate (hard)
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67742/F
| When: 2024-02-05 18:42:02
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
int n,ans = 0;
cin >> n;
set<int>col;
vector<priority_queue<int>>pos(n+1);
for(int i = 1;i <= n;i++){
int temp;
cin >> temp;
col.emplace(temp);
pos[temp].emplace(i);
}
while(!col.empty()){
auto tps = col;
ans++;
int now = 999999999;
for(auto c:col){
now = min(now,pos[c].top());
}
for(auto c:col){
while(!pos[c].empty() && pos[c].top() >= now){
pos[c].pop();
}
if(pos[c].empty())tps.erase(c);
}
col = tps;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}
F Tokitsukaze and Eliminate (hard)
题意
同上题
思路
同上题
代码
同上题
I Tokitsukaze and Short Path (plus)
题意
有一张
完全图指的是每对顶点之间都恰好有一条无向边的图。
对于顶点
定义
关于最短路的定义:
定义一条从
特别的,当
定义一条从
定义
思路
由分析得,如果想得到路径最短,联通所有点的过程中,要更多的重复经过边权小的边,尽可能少的重复经过边权大的边。
通过手动展开式子可得,发现 由
可得
代码
/*******************************
| Author: AlwaysBeShine
| Problem: Tokitsukaze and Short Path (plus)
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67742/I
| When: 2024-02-05 19:26:34
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
ll n,sum = 0;
cin >> n;
std::vector<ll> a(n+1);
for(int i = 1;i <= n;i++){
cin >> a[i];
}
sort(a.begin(),a.end());
for(int i = 1;i <= n;i++){
sum += a[i]*(i-1);
}
cout << 4 * sum << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}
J Tokitsukaze and Short Path (minus)
题意
和上题区别在于本题的定义有所更改
同求
思路
分析过程同上题
对式子展开后的分析有所不同,如果
则可知,任何两点间的路径边权关系,都可以转换到是否要绕路经过 最小边权 所对应的点。
在上题基础上稍加调整即可。
代码
/*******************************
| Author: AlwaysBeShine
| Problem: Tokitsukaze and Short Path (minus)
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67742/J
| When: 2024-02-05 19:36:09
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
ll n,sum = 0;
cin >> n;
std::vector<ll> a(n+1,0);
for(int i = 1;i <= n;i++){
cin >> a[i];
}
sort(a.begin(),a.end());
for(int i = 1;i <= n;i++){
if(a[i] > 2*a[1]){
sum += 2 * a[1]*(n-i);
}else{
sum += a[i]*(n-i);
}
}
cout << 4 * sum << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!