写在前面

几个家长要求我写一些2021百度之星初赛第一场的题解。

1003 鸽子

原题链接

https://acm.hdu.edu.cn/showproblem.php?pid=6998
http://47.110.135.197/problem.php?id=5977

简单题解

就是有些操作可做可不做。自然想到了01背包。因此可以使用动态规划。

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;const int MAXN=1e5+10;
LL dp[MAXN];//dp[j]为当前枚举的操作(含之前枚举过的操作)上,对于第j个位置的最小暗箱操作次数。void solve(){memset(dp, -1, sizeof(dp));LL n, m, K, u, v;cin>>n>>m>>K;dp[K]=0;while(m--){LL u,v;cin>>u>>v;if (dp[u]==-1&&dp[v]==-1) {//什么都不用做continue;} else if (dp[u]==-1&&dp[v]!=-1) {dp[u]=dp[v];dp[v]+=1;} else if(dp[u]!=-1&&dp[v]==-1){dp[v]=dp[u];dp[u]+=1;} else{LL t1=dp[v], t2=dp[u];dp[v]=min(dp[v]+1, t2);dp[u]=min(dp[u]+1, t1); }}for(LL i=1;i<=n;++i){//这里要特别注意,本题会卡最后一个空格。艹,PE了n次。cout<<dp[i];if (i!=n) {cout<<" ";}}cout<<"\n";
}int main(){#if 1ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#endifLL T;cin>>T;while (T--) {solve();}return 0;
}

1004 萌新

题目链接

https://acm.hdu.edu.cn/showproblem.php?pid=6999
http://47.110.135.197/problem.php?id=5974

题解

本题是一个数学题。
求 a m o d c = b m o d c a\ mod\ c=b\ mod\ c a mod c=b mod c,移项可得 ( a − b ) m o d c = 0 (a-b)\ mod\ c=0 (a−b) mod c=0。
为了不减少通用性,我们假设 a > b a>b a>b。
因此最大的 c c c 就是 a − b a-b a−b。下面我们需要对 c c c 的最小值进行讨论。
当 a = = b a==b a==b 时候,最小的 c c c 为 2 2 2。
当 a − b = = 1 a-b==1 a−b==1 时候,也就是 a a a 和 b b b 互质,不存在最大和最小的 c c c。
当 a − b > 1 a-b>1 a−b>1 时候,暴力枚举即可最小的 c c c 即可。如果找不到最小的 c c c,不存在最大和最小的 c c c。
注意我们需要将时间复杂度控制在 O ( T × l o g ( a − b ) ) O(T \times log(a-b)) O(T×log(a−b))。

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;int main() {#if 1ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#endif//freopen("00.in", "r", stdin);//freopen("00.out", "w", stdout);LL T;cin>>T;while (T--) {LL a,b;cin>>a>>b;//a%c=b%c -> (a-b)%c=0LL maxx=abs(a-b);if (0==maxx) {//a==bif (a>1) {cout<<"2 "<<a<<"\n";} else {cout<<"-1 -1\n";}} else if (1==maxx) {cout<<"-1 -1\n";} else {//暴力枚举bool flag=true;for (LL i=2; i*i<=maxx; i++) {if (maxx%i==0) {flag=false;cout<<i<<" "<<maxx<<"\n";break;}}if (flag) {cout<<maxx<<" "<<maxx<<"\n";}}}return 0;
}

1006 毒瘤的数据结构

题目链接

https://acm.hdu.edu.cn/showproblem.php?pid=7001
http://47.110.135.197/problem.php?id=5976

题解

考点数据结构的双向链表。
由于本题的数据比较大,5e6。自然不能去一一遍历。因此本题可以考虑使用双向链表或者并查集来完成。
道歉目前该题处于 TLE 状态,应该是 5e6 数据读取问题。已经解决
我算服了,HDU 服务器只能使用 fread() 方式通过,其他方式读取,全部都是 TLE。
为了这个 TLE,搞了几天,都要怀疑人生了。不过还是自己对这样海量数据读取不了解问题。

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;const int MAXN=5e6+10;
bool aa[MAXN];
LL nex[MAXN];
LL pre[MAXN];namespace fastIO {static char buf[100000],*h=buf,*d=buf;//缓存开大可减少读入时间,看题目给的空间#define gc h==d&&(d=(h=buf)+fread(buf,1,100000,stdin),h==d)?EOF:*h++//不能用fread则换成getchartemplate<typename T>inline void read(T&x) {int f = 1;x = 0;register char c(gc);while(c>'9'||c<'0'){if(c == '-') f = -1;c=gc;}while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+(c^48),c=gc;x *= f;}template<typename T>void output(T x){if(x<0){putchar('-');x=~(x-1);}static int s[20],top=0;while(x){s[++top]=x%10;x/=10;}if(!top)s[++top]=0;while(top)putchar(s[top--]+'0');}
}
using namespace fastIO;int main(){LL n,op,x;read(n);pre[n+1]=n;nex[0]=1;for (LL i=1;i<=n;++i) {nex[i]=i+1;pre[i]=i-1;}for (LL i=1;i<=n;++i){LL op,x;read(op);read(x);if(op==1){//1操作if(aa[x]) {continue;}nex[pre[x]]=nex[x];pre[nex[x]]=pre[x];aa[x]=1;} else {if(x==nex[0]){cout<<nex[nex[0]]<<"\n";} else {cout<<nex[0]<<"\n";}}}return 0;
}

下面代码是使用并查集。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <utility>using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;namespace fastIO {static char buf[100000],*h=buf,*d=buf;//缓存开大可减少读入时间,看题目给的空间#define gc h==d&&(d=(h=buf)+fread(buf,1,100000,stdin),h==d)?EOF:*h++//不能用fread则换成getchartemplate<typename T>inline void read(T&x) {int f = 1;x = 0;register char c(gc);while(c>'9'||c<'0'){if(c == '-') f = -1;c=gc;}while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+(c^48),c=gc;x *= f;}template<typename T>void output(T x){if(x<0){putchar('-');x=~(x-1);}static int s[20],top=0;while(x){s[++top]=x%10;x/=10;}if(!top)s[++top]=0;while(top)putchar(s[top--]+'0');}
}
using namespace fastIO;const int MAXN=5e6+10;
LL rel[MAXN];
//并查集
LL fa[MAXN];
LL sa[MAXN];void init(LL n) {for (LL i=0; i<=n; i++) {fa[i]=i;sa[i]=1;}
}LL find(LL x) {if (fa[x]!=x) {fa[x]=find(fa[x]);}return fa[x];
}void merge(LL x, LL y) {x=find(x);y=find(y);if (x==y) {return;}fa[x]=y;sa[y]+=sa[x];
}int main() {#if 0ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#endif//freopen("00.in", "r", stdin);//freopen("00.out", "w", stdout);int n;read(n);init(n);rel[0]=1;for (int i=1; i<=n; i++) {int opt, x;read(opt);read(x);if (1==opt) {rel[x]=1;if (rel[x-1]==1) {merge(x-1, x);}if (rel[x]==rel[x+1]) {merge(x, x+1);}} else {int ans=find(1);if (ans>=x-1) {ans=find(x+1);if (ans<=n && rel[ans]==1) {ans++;}} else {ans=ans+rel[ans];}printf("%d\n", ans);}}return 0;
}

1008 猎人杀

题目链接

https://acm.hdu.edu.cn/showproblem.php?pid=7003
http://47.110.135.197/problem.php?id=5975

题解

模拟题。应该是这次初赛最简单的一题。见鬼,竟然放在了最后一题。

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;const int MAXN = 50+10;
LL a[MAXN];
LL b[MAXN][MAXN];
bool vis[MAXN];
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL T;cin >> T;while(T--){LL n;cin>>n;//清数据memset(vis,0,sizeof(vis));LL cnt = n;LL lr = -1;//狼人bool flag = 0;for(LL i = 1; i <= n; i++){cin >> a[i];b[i][1] = a[i]; if(a[i] == 1){lr = i;//找到狼的位置}}for(LL i = 1; i <= n; i++){for(LL j = 2; j <= n + 1; j++){cin >> b[i][j];}}LL k = 2;LL wz = b[lr][k];//第一晚 狼人杀人(可自刀) while(lr != -1 && cnt > 2){if(lr == wz){//狼自杀 lr = -1;if(lr == -1){cout <<"lieren\n";break;}} else{//杀猎人 while(!vis[wz]){//如果猎人还活着 vis[wz] = 1;//猎人死亡 if(wz == lr){//如果下个死亡的是狼的话 cout << "lieren\n";flag = 1;break;}cnt--;if(cnt <= 2){//如果只剩下两个玩家的话 cout <<"langren\n";flag = 1;break;}while(vis[b[wz][k]] == 1){//直到找出第wz行暗杀名单中最靠前且还活着的玩家。 k++;}wz = b[wz][k];//更新 k = 2;//将每个玩家的暗杀名单遍历下标重置。 }if(flag == 1){//游戏结束,跳出循环 break;}}}}return 0;
}

总结

还是太水了,还有些题需要进一步考虑一下。

2021百度之星初赛第一场部分题解相关推荐

  1. 51nod 1515 明辨是非 2017百度之星初赛第一场第二题(并查集+启发式合并)

    题目: 原题链接 给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变 ...

  2. 2017百度之星初赛第一场题解

    前言 这场比赛我卡在线上了,没有进TAT 我只做了三道水题.. 首先是在比赛开始的时候我还在睡觉,我以为是2:30开始.. 然后,由于这个垃圾评测,卡死人了.. 于是我刷新一下,就算了我交了两次,于是 ...

  3. [水]2015百度之星初赛第一场 超级赛亚ACMer

    Description 百小度是一个ACMer,也是一个超级赛亚人,每个ACMer都有一个战斗力,包括百小度.
所谓超级赛亚人的定义,是说如果在对抗中刚好接近极限状态,那就会激发斗志,实力提升.
 具 ...

  4. 2021百度之星初赛二(1001 -- 1003)

    2021百度之星初赛二(1001 – 1003) 1001 题意: 给 a,b,每次 a,b会变为 a+b,a-b,问 k 次之后变成了哪两个数,对 998244353998244353 取模,多组数 ...

  5. 2017百度之星初赛B场第一题Chess--简单杨辉三角问题

    Chess  Accepts: 1799  Submissions: 5738  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 32768 ...

  6. 2017百度之星初赛B场总结

    (A场因为不可抗力因素(?)没能参加,B场还好算是磕磕碰碰地吃着低保过去了,真的菜呀) Chess Problem Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋 ...

  7. 2014百度之星初赛第一轮解题报告:information

    Information 时间限制: 1s 内存限制: 65536K 问题描述 军情紧急,我们需要立刻开发出一个程序去处理前线侦察兵发回的情报,并做出相应的分析.现在由你负责其中的一个子模块,你需要根据 ...

  8. 2021百度之星初赛一7.31

    1008穿越隧道 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 60; ...

  9. 2021百度之星初赛 A迷失(DP+flody+二进制优化)

    link 题意: 小 T 迷失在了一个有 n 个点的群岛上. 初始时他在 1 号岛,他要通过架在岛间的 m 座双向桥,在正好过 k 座桥时达到 n 号岛的大门. 这些桥中有若干座附魔桥.当小 T 经过 ...

最新文章

  1. Gym-100889B Backward and Forward
  2. java xml获取属性值_java – 如何获取具体属性值的特定XML元素?
  3. python os操作
  4. python的ogr模块_python GDAL/OGR模块安装注意事项
  5. Oracle冷备迁移过程和在线日志损坏处理
  6. (?i) 和 re.sub
  7. Android 饼状图(MPAndroidChart框架)
  8. java 天上掉东西游戏的源代码_【小游戏】前两天的小游戏终于调试成功了。。。。直接源代码...
  9. ie9服务器win2008系统离线安装包,IE9离线安装包完整版
  10. android soundpool 封装,Android中使用SoundPool来播放音频
  11. C语言中三目运算符的结合性问题
  12. 梅西大学研究员创造出新3D打印系统 用螺杆作为进料机构挤出颗粒
  13. 裸机运行c语言,裸机_GPIO实验_C语言
  14. 字符对应的URL编码值集合
  15. 过滤对象属性值为空的属性
  16. 方法重载和重写的区别,以及如何体现了多态性
  17. 【20190405】算法-输入一个字符串,按字典序打印出该字符串中字符的所有排列
  18. <数据结构>倒拔二叉树
  19. Advanced Archive Password Recovery下载
  20. SpringBoot应用WebSocket实现在线聊天

热门文章

  1. 张量分解的鲁棒图像哈希
  2. 服务器防沉迷系统,《明日之后》未成年防沉迷系统上线,11个服务器“遭殃”!...
  3. MCE公司:Wnt (wingless) / β-catenin通路中的小分子抑制剂
  4. 拆解小米4C: 依照小米4i刻出来的安卓小王子---ESM
  5. token的基本使用
  6. word文档里面的空白页怎么删除
  7. 项目分享 | MindSpore Insight AI可视化工具开发心得
  8. Java内存管理与垃圾回收
  9. Java 8特性之Optional详解
  10. 增强灰狼和布谷鸟混合优化搜索算法(AGWO-CS)matlab仿真,提供20多个标准目标函数进行测试