题目来源: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. Qt opencv开发环境

    在.pro文件中添加 INCLUDEPATH += C:\opencv\build\include\ #头文件路径 C:\opencv\build\include\opencv\ C:\opencv\ ...

  2. does not contain bitcode. You must rebuild it with

    *** does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE ...

  3. Access restriction required library rt.jar

    在JAVA项目开发中,使用到了BASE64Decoder,但编辑运行时却会出现以下错误:Access restriction required library rt.jar,这里就详细的说明一下如何解 ...

  4. Maven核心概念(转)

    转自 https://www.cnblogs.com/xdp-gacl/p/4051819.html 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1 ...

  5. 托盘在XP下不能显示tooltip,在Vista和Windows7下正常

    转自:http://blog.csdn.net/debehe/article/details/4294053 奇怪的问题,想了很多可能的理由,最终的答案竟然是一开始就被我否认了的一种可能!! 问题现象 ...

  6. GCD学习(六) dispatch_async 和dispatch_sync

    dispatch_sync(),同步添加操作.他是等待添加进队列里面的操作完成之后再继续执行. dispatch_queue_t concurrentQueue = dispatch_queue_cr ...

  7. Entity Framework Tutorial Basics(18):DBEntityEntry Class

    DBEntityEntry Class DBEntityEntry is an important class, which is useful in retrieving various infor ...

  8. XML DTD语法详解

    XML DTD详解   XML DTD详解 前情提要与本文内容介绍 前面的两篇XML相关博文: 第一篇是介绍格式正规的XML: 格式正规的XML:语法 属性 实体 处理指令 样式单 CDATA节 第二 ...

  9. libtool的工作原理

    libtool 是一个通用库支持脚本,将使用动态库的复杂性隐藏在统一.可移植的接口中:使用libtool的标准方法,可以在不同平台上创建并调用动态库.可以认为libtool是gcc的一个抽象,其包装了 ...

  10. Adb无法连接Genymotion

    后来注意到Genymotion启动的时候adb无法正常使用,反复调用adb启动新的server也无法解决 解决起来很简单.打开Genymotion设置界面,进入ADB标签,选中Use custom A ...