POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)

题意:给定 n 个字符串,一个k,让你把它们分成每组k个,要保证每组中每个字符串长度与它们之和相差不能超2.

析:贪心策略就是长度相差最小的放上块。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <functional>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <deque>
#include <map>
#include <cctype>
#include <stack>
#include <sstream>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
char s[maxn];
vector<int> v;

int main(){
    int kase = 0;
    while(scanf("%d %d", &n, &m) == 2){
        if(!m && !n)  break;

        v.clear();
        for(int i = 0; i < n; ++i){
            scanf("%s", s);
            v.push_back(strlen(s));
        }
        sort(v.begin(), v.end());
        bool ok = true;
        for(int i = 0; i < n/m; i++){
            double cnt = 0.0;
            for(int j = 0; j < m; ++j)
                cnt += v[i*m+j];
            cnt /= (double)m*1.0;
            for(int j = 0; j < m; ++j)
                if(abs((double)v[i*m+j]*1.0 - cnt) > 2.0){ ok = false; break; }
        }
        if(kase++)  printf("\n");
        printf("Case %d: %s\n", kase, ok ? "yes" : "no");
    }
    return 0;
}

  

时间: 2024-12-21 22:03:00

POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)的相关文章

Sicily 1405. Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals

1405. Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The Association for Computing Machinery (ACM) is considering new rules for its regional programming c

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

poj 2369 Permutations 置换水题

找循环节求lcm就够了,若答案是12345应该输出1,被坑了下. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; #define INF 0x3FFFFFF #define MAXN 2222 #define eps 1e-6 i

poj 1005:I Think I Need a Houseboat(水题,模拟)

I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85149   Accepted: 36857 Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land,

POJ 3030. Nasty Hacks 模拟水题

Nasty Hacks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13136   Accepted: 9077 Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends.

POJ 2545+2591+2247+1338简单水题

[题意简述]:就是有这样的一个序列,就拿当p1 = 2,p2 = 3, p3 = 5,来举例,由这三个数为基准组成的序列是: 2,3,4,5,6,8,9,10,12--现在给你这个序列数组的下标,让你求得这个数组中,这个下标里的数是多少. [分析]:2,3,4,5,6,8,9,10,12--这个序列式由2,3,5这三个数生成的,具体如何生成,就是: 详见代码: 这里以POJ2545为例: //弄清其中的逻辑关系,可以从最简单的2,3,5试着做起! #include<iostream> #inc

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

poj 1002:487-3279(水题,提高题 / hash)

487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 236746   Accepted: 41288 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phras

POJ 1488 Tex Quotes --- 水题

POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 Tex Quotes --- 水题 */ #include <cstdio> #include <cstring> int main() { #ifdef _LOCAL freopen("D:\\input.txt", "r", stdin); #