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

    你可能感兴趣的文章
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>