UVA10273 - Eat or Not to Eat?(暴力)

题目链接

题目大意:一个农场的主人为了改善收入决定每天要将产量最少的那一头牛杀掉,但是如果这一天出现了多只产量最小的牛,那么这一天一头也不杀。然后给你n头牛,给你每头牛的产量周期和周期内的产量,问多少天后可以确定剩余的牛的情况。

解题思路:求出这n头牛的周期的最小公倍数,那么在这个周期内,如果没有牛被杀的话,那么之后的情况是循环的,不会再有牛被杀了,这样情况就确定下来了。在这样的条件下,暴力就不会超时。

代码:

#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1005;
const int M = 15;
const int maxn = 255;

int Milk[N][M];
int cnt[N];
int vis[N];
int survice[N];
int n;

int gcd (int a, int b) {

    return b == 0 ? a : gcd (b, a % b);
}

int f() {

    int T = 1;
    int tmp;
    for (int i = 0; i < n; i++) {
        if (vis[i])
            continue;
        if (T > cnt[i])
            tmp = gcd(T, cnt[i]);
        else
            tmp = gcd(cnt[i], T);
        T = T * cnt[i] / tmp;
    }
    return T;
}

int solve () {

    int T = f();
    memset (vis, 0, sizeof (vis));
    //    memset (survice, -1, sizeof (survice));
    survice[n] = 0;
    int num = n;
    for (int p = 0;; p++) {

        bool flag = 0;
        for (int i = 1; i <= T; i++) {

            int minnum = maxn;
            int tmp = 0;
            for (int j = 0; j < n; j++) {

                if (vis[j])
                    continue;
                int d = (i % cnt[j]) == 0 ? cnt[j] : (i % cnt[j]);
                if (minnum > Milk[j][d]) {
                    minnum = Milk[j][d];
                    tmp = j;
                } else if (minnum == Milk[j][d])
                    tmp = -1;
            }

            if (tmp != -1) {
                vis[tmp] = 1;
                num--;
                survice[num] = p * T + i;;
                flag = 1;
            }
        }
        if (!flag)
            return num;
    }
}

int main () {

    int cas, d;
    scanf ("%d", &cas);
    while (cas--) {

        scanf ("%d", &n);
        for (int i = 0; i < n; i++) {

            scanf ("%d", &cnt[i]);
            for (int j = 1; j <= cnt[i]; j++)
                scanf ("%d", &Milk[i][j]);
        }
        int ans = solve();
        printf ("%d %d\n", ans, survice[ans]);
    }
    return 0;
}
时间: 2025-01-14 06:54:51

UVA10273 - Eat or Not to Eat?(暴力)的相关文章

UVA 10273 Eat or not to Eat?

直接暴力模拟 .以每次还未被删除的cnt[i]为一周期进行暴力模拟 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdi

函数的链式调用实现Man().sleep().eat()

let index = 0; let stack = []; function next() { let fn = stack[index]; index++; if(typeof fn === 'function'){ fn(); } } function fn1() { console.log(1) next() } function fn2() { setTimeout(function() { console.log(2) next() },0) } function fn3() { c

python基础--常用模块与面向对象基础

1常用模块 1.1 xml xml是实现不同语言或程序之间进行数据交换的协议 xml的格式如下: <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdp

Project Euler 98:Anagramic squares 重排平方数

Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively, we form a square number: 1296 = 362. What is remarkable is that, by using the same digital substitutions, the anagram, RACE, also forms a square num

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

java新特性下

泛型 为什么需要泛型,什么是泛型? 对象类型数据保存到集合中时,会丢失其类型,取出时变成Object类型,Object类型需要强转回丢失的数据类型,这容易导致程序员的误操作问题,带来安全性!泛型就是给集合强制其只能保存一种数据类型,从而不会出现元素数据类型丢失问题!! package 泛型; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Linke

49. Group Anagrams同义词合并

[抄题]: Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat"

CVE-2013-2551

目录 小白的CVE-2013-2551 分析 & 利用 0xFF 前言 0x00 环境和工具 0x01 分析POC POC 调试 0x02 利用 构造R3任意内存读写 劫持eip 利用利用 0x03 总结: 0x04 参考 小白的CVE-2013-2551 分析 & 利用 0xFF 前言 小白第一次尝试来分析浏览器的漏洞,在此之前我不会html,javascript,css,总之跟网站有关的我都不会.然后我花了一天去看与之相关的东西.学习了下javascript的基本语法.总之浏览器的东西