题目来源:http://poj.org/problem?id=1055

题目大意:

  每封信都有一个zip-code, 由5位数字构成,可以通过将zip-code相同或相近的信件打包来节省成本。打包规则是:5位数字完全相同的10-15封可组成一个包(5-digit bundles),或者将前3位数字相同的信件打包,同样10-15份一包(3-digit bundles)。优先分配为5-digit bundles, 其次3-digit bundles。不能被打包的信件为first class letters。要求输出打包方案。

输入:没行一个zip-code,但并非每个都是合法的。合法的zip-code恰好由5位数字组成,不能全为0.

输出:按上述要求输出打包方案,格式见输出,合法的bundles和letters需要按数字大小顺序输出。。


Sample Input

95864
95864
95864
95867
95920
9j876
95616
95616
95747
95814
95818
95818
8976
95818
95818
95819
95819
00000
95819
95819
95819
95819
95819
95825
95825
95825
95825
95825
95826
95826
95826
95826
95826
95826
95827
8976
95833
95833
95833
95833
95819
95819
95819
95819
95833
95833
95833
95864
95864
95864
123456
95864
95864
95864
95864

Sample Output

ZIP         LETTERS     BUNDLES

95819          11           1
95864 10 1 958xx 25 2 95616 2 0
95747 1 0
95920 1 0 TOTALS 50 4 INVALID ZIP CODES 9j876
8976
00000
123456

本题虽在POJ的第一版,但是却人气颇低..确实不太有意思, 主要思想就是桶排序。

输出格式非常坑人,很容易错,题目描述得也不是特别清楚,最后的非法zip-code需要判重且按出现顺序输出。按照提述规则,似乎有可能出现多解的情况,但是测试数据没有太刁难,invalid zip-code的长度最长只到6,zip-code最高位不为0,用最简单的策略(对于一些比较偏的case可能不能过的)也可以AC。所以,这样的题随便看看就好了..

 ////////////////////////////////////////////////////////////////////
// POJ1055 BULK MAILING
// Memory: 1212K Time: 0MS
// Language: C++ Result : Accepted
/////////////////////////////////////////////////////////////////// #include <cstdio>
#include <string>
//#include <algorithm> using namespace std; struct Bundle {
int letter_cnt, bun_cnt;
}; Bundle buns[];
char buffer[], invalid_record[][];
int invalid_cnt, zip_cnt[], total_letters, total_buns; bool M[]; bool validity_check(char buffer[]){ //位数检查
if (strlen(buffer) != ) {
return false;
} //数字检查
for (int i = ; i < ; i++) {
if (buffer[i] < '' || buffer[i] > '') {
return false;
}
} //全0检查
bool flag = true;
for (int i = ; i < ; ++i) {
if (buffer[i] != '') {
flag = false;
break;
}
}
if (flag) {
return false;
}
return true;
} void process1(void) { //同5位10份以上打包
for (int i = ; i <= ; ++i) {
if (zip_cnt[i] >= ){
while (zip_cnt[i] >= ) {
buns[i].letter_cnt += ;
++buns[i].bun_cnt;
total_letters += ;
++total_buns;
zip_cnt[i] -= ;
}
if (zip_cnt[i] >= ) {
buns[i].letter_cnt += zip_cnt[i];
++buns[i].bun_cnt;
total_letters += zip_cnt[i];
++total_buns;
zip_cnt[i] = ;
}
}
} //按序输出
printf("ZIP LETTERS BUNDLES\n");
puts("");
for (int i = ; i <= ; ++i) {
if (buns[i].letter_cnt != ) {
printf("%d%12d%12d\n", i, buns[i].letter_cnt, buns[i].bun_cnt);
}
}
puts(""); //清空三位数的桶
for (int i = ; i <= ; ++i) {
buns[i].letter_cnt = buns[i].bun_cnt = ;
}
} void process2(void) { //同3位10份以上打包
for (int i = ; i <= ; ++i) {
int temp[][];
int cnt = , letter_cnt = ;
for (int j = ; j <= ; ++j) {
int num = i * + j;
if (zip_cnt[num] > ) {
temp[cnt][] = num;
temp[cnt][] = zip_cnt[num];
letter_cnt += zip_cnt[num];
zip_cnt[num] = ;
++cnt;
}
}
while (letter_cnt >= ) {
++buns[i].bun_cnt;
++total_buns;
buns[i].letter_cnt += ;
total_letters += ;
letter_cnt -= ;
}
if (letter_cnt >= ) {
++buns[i].bun_cnt;
++total_buns;
buns[i].letter_cnt += letter_cnt;
total_letters += letter_cnt;
letter_cnt = ;
}
while (letter_cnt > ) {
if (temp[cnt - ][] >= letter_cnt) {
zip_cnt[temp[cnt - ][]] += letter_cnt;
break;
} else {
zip_cnt[temp[cnt - ][]] += temp[cnt - ][];
letter_cnt -= temp[cnt - ][];
--cnt;
}
}
if (buns[i].letter_cnt > ) {
printf("%dxx%12d%12d\n", i, buns[i].letter_cnt, buns[i].bun_cnt);
}
}
puts("");
} void process3(void) {
//first class 输出
for (int i = ; i <= ; ++i) {
if (zip_cnt[i] > ){
printf("%d%12d%12d\n", i, zip_cnt[i], );
total_letters += zip_cnt[i];
}
}
puts("");
} void output_invalid(void) {
//非法zip-code输出
printf("INVALID ZIP CODES\n\n");
for (int i = ; i < invalid_cnt; ++i) {
bool flag = true;
for (int j = ; j < i; ++j) {
if (strcmp(invalid_record[i], invalid_record[j]) == ) {
flag = false;
}
}
if (flag) {
printf("%s\n", invalid_record[i]);
}
} }
int main(void) {
invalid_cnt = ;
while (scanf("%s", buffer) != EOF) {
if (validity_check(buffer)) { //桶排序
int num = ;
for (int i = ; i < ; ++i) {
num = num * + buffer[i] - '';
}
++zip_cnt[num];
} else {
strcpy(invalid_record[invalid_cnt], buffer);
++invalid_cnt;
}
} process1();
process2();
process3();
printf("TOTALS%11d%12d\n\n", total_letters, total_buns);
output_invalid(); return ;
}

POJ1055 BULK MAILING的更多相关文章

  1. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  2. Bulk Insert:将文本数据(csv和txt)导入到数据库中

    将文本数据导入到数据库中的方法有很多,将文本格式(csv和txt)导入到SQL Server中,bulk insert是最简单的实现方法 1,bulk insert命令,经过简化如下 BULK INS ...

  3. Elasticsearch —— bulk批量导入数据

    在使用Elasticsearch的时候,一定会遇到这种场景--希望批量的导入数据,而不是一条一条的手动导入.那么此时,就一定会需要bulk命令! 更多内容参考我整理的Elk教程 bulk批量导入 批量 ...

  4. [Oracle] Bulk Insert Data

    命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NET for .NET Framew ...

  5. Oracle forall bulk collect批量数据更新

    对于数据量较大的插入操作可采用此种方法操作,注意: limit减少内存占用,如果数据量较大一次性全部加载到内存中,对PGA来说压力太大,可采用limit的方法一次加载一定数量的数据,建议值通常为100 ...

  6. BULK操作减少redo实验

    建表: create table sm_histable ( sm_id ), sm_subid ), service_type ), orgton ), orgnpi ), destton ), d ...

  7. sql 中的Bulk和C# 中的SqlBulkCopy批量插入数据 ( 回顾 and 粗谈 )

    通常,我们会对于一个文本文件数据导入到数据库中,不多说,上代码. 首先,表结构如下.   其次,在我当前D盘中有个文本文件名为2.txt的文件. 在数据库中,可以这样通过一句代码插入. Bulk in ...

  8. 笔记整理之 Bulk Insert

    之前2篇日志整理了BCP大致的用法,这次整理一下它的兄弟 Bulk Insert 的写法以及和bcp那边的结合的用法. 首先,Bulk Insert 语句要在连接了Sql Server 服务器之后才执 ...

  9. bulk collect no_data_found exception

    Bulk collect当没有数据抛出异常跟implicit cursor 处理不一样. 先看一下implicit cursor的处理吧: cl scr; DECLARE l_descr hardwa ...

随机推荐

  1. Firemonkey Android IOS 图标

    图标很多

  2. 蓝牙服务 UUID

    https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx 手机蓝牙对手机 ,华为平板取红米手机 8 个Audio So ...

  3. 斐波那契数列-java实现

    1,1,2,3,5,8,13,21...... 以上的数列叫斐波那契数列,今天的面试第一题,输出前50个,这里记录下. 方式一 package com.geenk.demo.my; /** * @au ...

  4. Windows平台上通过git下载github的开源代码

    常见指令整理: (1)检查ssh密钥是否已经存在.GitBash. 查看是否已经有了ssh密钥:cd ~/.ssh.示例中说明已经存在密钥 (2)生成公钥和私钥 $ ssh-keygen -t rsa ...

  5. import requests

  6. Mongo client - cross-platform MongoDB management tool

    Mongo client for Ubuntu or Windows http://robomongo.org/download.html

  7. 《Effective Java》第6章 枚举和注解

    第30条:用enum代替int常量 将加班工资计算移到一个私有的嵌套枚举中,将这个策略枚举(strategy enum)的实例传到PayrollDay枚举的构造器中.之后PayrollDay枚举将加班 ...

  8. 《Head First Servlets & JSP》-4-请求和响应

    Servlet生命周期 生命周期三大重要时刻 Servlet集成结构: 方法 init() service() doGet()/goPost() 何时调用 Servlet实例被创建之后.为客户请求提供 ...

  9. 树莓派(Raspberry Pi 3) 使用wifi模块连接网络

    树莓派3B内置了wifi和蓝牙模块,启动WIFI模块有两种方式,一种是图形界面,一种是命令行模式. 使用图形界面: 在桌面右上角的菜单栏里面选择wifi,输入密码就可以了. 使用命令行: 第一步:配置 ...

  10. Server.MapPath方法的应用方法

    老是忘记Server.MapPath的使用方法了,下面记录一下,以备后用:总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径1.Server.MapPath(&q ...