博客
关于我
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/

    你可能感兴趣的文章
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>
    Node-RED中使用node-red-contrib-image-output节点实现图片预览
    查看>>
    Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
    查看>>
    Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node-RED中建立Websocket客户端连接
    查看>>
    Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
    查看>>
    node-request模块
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>
    Node.js 函数是什么样的?
    查看>>