题解 | 牛客周赛 Round 86 - A~E

小苯跑外卖

https://ac.nowcoder.com/acm/contest/104637/A

个人题解 A~E

A

#include <iostream>
using namespace std;

int main() {
  int x,y;
  cin>>x>>y;
  cout<<(y+x-1)/x<<'\n';
  
  return 0;
}

B

诈骗?

#include <iostream>
using namespace std;

int main() {
  int t;
  cin>>t;
  while (t--) {
    int n,k,x;
    cin>>n>>k;
    long long ans = 0;
    while (n--)
      cin>>x, ans += (x > 0 ? x : 0);
    cout<<ans<<'\n';
  }
  
  return 0;
}

C

用stack试了试 诶 结果就过了
直接先贪心地消除 所有相邻的 可以消除的字符
最后剩余01相间的串 0101...
这个串需要修改length/2次

#include <iostream>
#include <stack>
using namespace std;

int main() {
  int t;
  cin>>t;
  while (t--) {
    int n;
    string s;
    cin>>n>>s;
    stack<char> stk;
    for (auto &ch: s)
      if (!stk.empty() && stk.top() == ch)
        stk.pop();
      else
        stk.push(ch);
    cout<<stk.size()/2<<'\n';
  }
  
  return 0;
}

D

盲猜结果不会太大 因为gcd使得数据减小的速度非常快(实际上是因为次数不会超过3)
直接用个队列暴力bfs 就过了

看了讲解 原来是构造题 因为是可重集合 任意用两次同一个操作x op y并加到集合中 再把这两个数异或就得到了0 所以答案不会超过3 剩下直接枚举即可
不如直接枚举((

#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int t;
  cin>>t;
  while (t--) {
    int x,y;
    cin>>x>>y;
    queue<vector<int>> que;
    que.push({x,y});
    while (!que.empty()) {
      auto vec = que.front();
      que.pop();
      for (int i=0; i<vec.size(); i++)
        for (int j=i+1; j<vec.size(); j++) {
          if ((vec[i]&vec[j]) == 0 || (vec[i]^vec[j]) == 0) {
            cout<<vec.size()-1<<'\n';
            goto end;
          }
          int g = gcd(vec[i], vec[j]), a[4] = {vec[i]&vec[j], vec[i]|vec[j], vec[i]^vec[j], g};
          for (int i=0; i<4; i++)
            vec.push_back(a[i]), que.push(vec), vec.pop_back();
        }
    }
    end:;
  }
  
  return 0;
}

E

先对所有边排序
枚举所有的最长边 对所有比它小的边 用类似多项式生成函数的方法(或者说背包dp) 记录组合出的所有可能值 可以用bitset+移位实现
然后贪心地找第一个大于这条边的长度

#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;

int main() {
  int t;
  cin>>t;
  while (t--) {
    int n,a[102];
    cin>>n;
    for (int i=1; i<=n; i++)
      cin>>a[i];
    sort(a+1, a+1+n);
    bitset<202> b;
    b[0] = b[a[1]] = b[a[2]] = b[a[1]+a[2]] = 1;
    int ans = 2e9, mx = a[1]+a[2];
    for (int p=3; p<=n; p++) {
      for (int i=a[p]+1; i<=mx; i++)
        if (b[i]) {
          ans = min(ans, a[p]+i);
          break;
        }
      mx = max(mx, mx+a[p]);
      b |= b<<a[p];
    }
    cout<<(ans == int(2e9) ? -1 : ans)<<'\n';
  }
  
  return 0;
}
全部评论
主要是因为构造的做法是有旧的和旧的多次进行运算的情况,也就是x 和 y做两次相同的运算
点赞 回复 分享
发布于 2025-03-24 15:17 河北
看来下d的题解,发现一直是旧的和新的做运算,然后放入队列中,为什么没有旧的和旧的再做一次运算呢,情况是否枚举的不够全面,但是为啥又能过
点赞 回复 分享
发布于 2025-03-24 15:15 河北

相关推荐

目前大二,中流211,这个简历能找到实习嘛?想知道八股要背到什么程度能开始投简历呢tot能投大厂吗
牛客44176770...:兄弟,先不急着找大厂实习吧,真的😭面试问的挺深的,会表面八股根本行不通,你的项目人家都没心情问😭小厂实习可以去吧,我就是有个小厂实习才有一个且仅有一个约面机会。而且,一开始就先问算法题,然后是八股MySQLRedis这些。你平时看这些,以为自己会了,但是在面试中问你,对于MySQL你了解多少,你能在面试中有逻辑的回答出来吗?而且,你大二,人家对你的项目根本不感兴趣,只问你基础。唉今天刚结束百度一面,我这几天都重点复盘了做过的项目,结果根本不问,问就问一个lua脚本怎么写。😭真的兄弟,咱不是天才,真得一步一步来,扎实基础😔😔
点赞 评论 收藏
分享
评论
11
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务