【逐日一题 | 2025年5.5 ~ 5.11】搜索相干题

[复制链接]
发表于 4 小时前 | 显示全部楼层 |阅读模式

   个人主页:Guiat
归属专栏:逐日一题

  



正文
1. 【5.5】P3717 [AHOI2017初中组] cover

   题目链接:https://www.luogu.com.cn/problem/P3717
  【分析】
暴力搜索 + 两点间隔公式,时间复杂度:O(m * n ^ 2)。
【AC_Code】
  1. #include <iostream>
  2. #include <cmath>
  3. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  4. using namespace std;
  5. bool vis[105][105]; int ans;
  6. void solve()
  7. {
  8.         int n, m, r; cin >> n >> m >> r;
  9.         while (m --)
  10.         {
  11.                 int x, y; cin >> x >> y;
  12.                 for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++)
  13.                 {
  14.                         double dis = sqrt(pow((i - x), 2) + pow((j - y), 2));
  15.                         if (dis <= r) vis[i][j] = true;
  16.                 }
  17.         }
  18.         for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) if (vis[i][j]) ans ++;
  19.         cout << ans << '\n';
  20. }
  21. int main()
  22. {
  23.         IOS int _ = 1;   // cin >> _;
  24.         while (_ --) solve();
  25.        
  26.         return 0;
  27. }
复制代码
2. 【5.6】P1897 电梯里的尴尬

   题目链接:https://www.luogu.com.cn/problem/P1897
  【分析】
先读懂题目样例,之后按题意模拟即可,具体看代码


【AC_Code】
  1. #include <iostream>
  2. #include <algorithm>
  3. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  4. using namespace std;
  5. const int N = 1e5 + 10; int a[N], now, ans;
  6. void solve()
  7. {
  8.         int n; cin >> n; for (int i = 0; i < n; i ++) cin >> a[i];
  9.         sort(a, a + n);
  10.         for (int i = 0; i < n; )
  11.         {
  12.                 int tar = a[i];
  13.                 if (tar > now) ans += (tar - now) * 6;
  14.                 else if (tar < now) ans += (now - tar) * 4;
  15.                 int cnt = 0;
  16.                 while (i < n && a[i] == tar) cnt ++, i ++;
  17.                 ans += 5; ans += cnt; now = tar;
  18.         } ans += now * 4;
  19.         cout << ans << '\n';
  20. }
  21. int main()
  22. {
  23.         IOS int _ = 1;   // cin >> _;
  24.         while (_ --) solve();
  25.        
  26.         return 0;
  27. }
复制代码
3. 【5.7】P2689 东南西北

   题目链接:https://www.luogu.com.cn/problem/P2689
  【分析】
按题意模拟即可,注意逻辑,属于简单题。
【AC_Code】
  1. #include <iostream>
  2. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  3. using namespace std;
  4. int ans;
  5. void solve()
  6. {
  7.         int x1, y1, x2, y2, T; cin >> x1 >> y1 >> x2 >> y2 >> T;
  8.         while (T --)
  9.         {
  10.                 char c; cin >> c;
  11.                 if (c == 'N' && y1 < y2) y1 ++, ans ++;
  12.                 else if (c == 'S' && y1 > y2) y1 --, ans ++;
  13.                 else if (c == 'W' && x1 > x2) x1 --, ans ++;
  14.                 else if (c == 'E' && x1 < x2) x1 ++, ans ++;
  15.                 if (x1 == x2 && y1 == y2) { cout << ans << '\n'; return ; }
  16.         }
  17.         cout << -1 << '\n';
  18. }
  19. int main()
  20. {
  21.         IOS int _ = 1;   // cin >> _;
  22.         while (_ --) solve();
  23.        
  24.         return 0;
  25. }
复制代码
4. 【5.8】P1145 约瑟夫

   题目链接:https://www.luogu.com.cn/problem/P1145
  【分析】

pos(删除位置)
4 = (0 + 4) % (2 * 3 - 0)
3 = (4 + 4) % (2 * 3 - 1)
3 = (3 + 4) % (2 * 3 - 2)
=> pos = (pos + m - 1) % (2 * k - i)

【AC_Code】
  1. #include <iostream>
  2. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  3. using namespace std;
  4. void solve()
  5. {
  6.         int k; cin >> k; int m = k + 1;
  7.         while (1)
  8.         {
  9.                 int pos = 0; bool flag = true;
  10.                 for (int i = 0; i < k; i ++)
  11.                 {
  12.                         pos = (pos + m - 1) % (2 * k - i);
  13.                         if (pos < k) { flag = false; break; }
  14.                 }
  15.                 if (flag) { cout << m << '\n'; return ; }
  16.                 m ++;
  17.         }
  18. }
  19. int main()
  20. {
  21.         IOS int _ = 1;   // cin >> _;
  22.         while (_ --) solve();
  23.        
  24.         return 0;
  25. }
复制代码
5. 【5.9】P1088 [NOIP 2004 普及组] 火星人

   题目链接:https://www.luogu.com.cn/problem/P1088
  【分析】
相称于一个全排列模版:求输入的整数序列举行 m 次字典序下一个排列的变动。
【AC_Code】
  1. #include <iostream>
  2. #include <algorithm>
  3. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  4. using namespace std;
  5. const int N = 1e4 + 10; int a[N];
  6. void solve()
  7. {
  8.         int n, m; cin >> n >> m;
  9.         for (int i = 0; i < n; i ++) cin >> a[i];
  10.         for (int i = 0; i < m; i ++) next_permutation(a, a + n);
  11.         for (int i = 0; i < n; i ++) cout << a[i] << ' '; cout << '\n';
  12. }
  13. int main()
  14. {
  15.         IOS int _ = 1;   // cin >> _;
  16.         while (_ --) solve();
  17.        
  18.         return 0;
  19. }
复制代码
6. 【5.10】P1164 小A点菜

   题目链接:https://www.luogu.com.cn/problem/P1164
  【分析】
01背包的变种,构建二维dp矩阵,f[j]表示前i个菜品恰恰花费j元的方案数,二维可以优化到一维,这里采用二维解法。
【AC_Code】
  1. #include <iostream>
  2. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  3. using namespace std;
  4. int a[110], f[110][10010];
  5. void solve()
  6. {
  7.         int n, m; cin >> n >> m; for (int i = 1; i <= n; i ++) cin >> a[i];
  8.         for (int i = 0; i <= n; i ++) f[i][0] = 1;
  9.         for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++)
  10.         {
  11.                 f[i][j] += f[i - 1][j];
  12.                 if (j >= a[i]) f[i][j] += f[i - 1][j - a[i]];
  13.         }
  14.         cout << f[n][m] << '\n';
  15. }
  16. int main()
  17. {
  18.         IOS int _ = 1;   // cin >> _;
  19.         while (_ --) solve();
  20.        
  21.         return 0;
  22. }
复制代码
7. 【5.11】P1019 [NOIP 2000 提高组] 单词接龙

   题目链接:https://www.luogu.com.cn/problem/P1019
  【分析】
考察搜索+字符串处理。
【AC_Code】
  1. #include <iostream>
  2. #include <string>
  3. #define IOS ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
  4. using namespace std;
  5. string s[25]; int n, vis[25], ans;
  6. string check(string s1, string s2)
  7. {
  8.         for (int i = 1; i < s1.length() && i < s2.length(); i ++)
  9.         {
  10.                 if (s1.substr(s1.length() - i, i) == s2.substr(0, i))
  11.                         return (s1.substr(0, s1.length() - i) + s2);
  12.         }
  13.         return "0";
  14. }
  15. void dfs(string drag)
  16. {
  17.         if (drag.size() > ans) ans = drag.size();
  18.         for (int i = 0; i < n; i ++)
  19.         {
  20.                 if (vis[i] == 2) continue;
  21.                 if (check(drag, s[i]) != "0") { vis[i] ++; dfs(check(drag, s[i])); vis[i] --; }
  22.         }
  23. }
  24. void solve()
  25. {
  26.         cin >> n; for (int i = 0; i < n; i ++) cin >> s[i];
  27.         char c; cin >> c;
  28.         for (int i = 0; i < n; i ++)
  29.         {
  30.                 if (s[i][0] == c) { vis[i] ++; dfs(s[i]); vis[i] --; }
  31.         }
  32.         cout << ans << '\n';
  33. }
  34. int main()
  35. {
  36.         IOS int _ = 1;   // cin >> _;
  37.         while (_ --) solve();
  38.        
  39.         return 0;
  40. }
复制代码
结语
感谢您的阅读!等候您的一键三连!接待指正!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表