博客
关于我
agc018B Sports Festival
阅读量:300 次
发布时间:2019-03-01

本文共 2127 字,大约阅读时间需要 7 分钟。

为了解决这个问题,我们需要找到一个运动集合,使得每个人在这个集合中可以选择他们最喜欢的运动,并且参加人数最多的那个运动的参加人数最少。

方法思路

我们可以使用贪心算法来解决这个问题。具体步骤如下:

  • 初始化:我们需要一个数组来记录每个运动被选中的人数。
  • 遍历每个运动:对于每个运动,检查它是否是当前最大的运动。
  • 更新最小值:每次找到当前最大的运动,记录它的参加人数,并更新答案。
  • 标记已处理的运动:将已经处理的运动标记为已选中,继续处理下一个运动。
  • 这种方法确保了每次处理一个运动,并记录最小的最大人数,从而得到最优解。

    解决代码

    #include 
    #include
    using namespace std;int read() { int x = 0, f = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') { f = -f; } ch = getchar(); } while ((ch >= '0') && (ch <= '9')) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f;}const int maxn = 300;const int inf = 0x3f3f3f3f;int n, m, a[maxn + 4][maxn + 4], in[maxn + 4], mx[maxn + 4], ans;int main() { n = read(); m = read(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { a[i][j] = read(); } } ans = n; int cnt = 0; while (cnt < m) { vector
    current; for (int j = 1; j <= m; ++j) { mx[j] = 0; } int now = -1; for (int i = 1; i <= n; ++i) { int k = 1; bool found = false; for (k = 1; k <= m; ++k) { if (!in[a[i][k]]) { found = true; break; } } if (found) { break; } current.push_back(k); } if (current.empty()) { break; } int max_val = 0; int max_j = -1; for (int j = 1; j <= m; ++j) { if (mx[j] > max_val) { max_val = mx[j]; max_j = j; } } if (max_j == -1) { break; } ans = min(ans, max_val); for (int j = 1; j <= m; ++j) { if (in[j] == 0 && mx[j] == max_val) { in[j] = 1; cnt++; } } } printf("%d\n", ans); return 0;}

    代码解释

  • 读取输入:函数read()用于读取输入数据,处理整数。
  • 初始化变量nm分别表示人数和运动种类,a数组存储每个人的喜好顺序。
  • 贪心算法:通过循环处理每个运动,找到当前最大的运动,记录参加人数,并更新最小值。
  • 输出结果:打印最终的最小最大人数。
  • 这种方法确保了我们每次处理一个运动,并记录最小的最大人数,从而得到最优解。

    转载地址:http://ccwo.baihongyu.com/

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>