题目来源: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. list array解析(总算清楚一点了)

    # -*- coding: utf-8 -*- """ Created on Tue Aug 09 23:04:51 2016 @author: Administrato ...

  2. 前端seo小结,网页代码优化

    seo的目的:提高网站流量 search engine optimization 搜索引擎优化seo search engine marketing 搜索引擎营销sem 权重10个等级 等级越大,权重 ...

  3. [Python Study Notes]pynput实现对键盘控制与监控

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  4. php入门学习

    尤其不认可W3school之类的东西,不够深度,理解不深,比起这个更建议看官方文档,中文不清楚,看英文的. 入门视频:入门视频推荐:哈佛大学公开课:构建动态网站Beginner PHP and MyS ...

  5. 批处理基本知识以及进阶 V2.0

    批处理基本知识以及进阶 将以要执行的程序指令 , 像在 dos 模式下一下写入记事本 , 保存成 bat 文件 , 就可以执行了 一 . 简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求 ...

  6. 34- 24 Point game

    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=43 24 Point game 时间限制:3000 ms  |  内存限制:65535 KB ...

  7. 业务逻辑: Quartz的整合应用

    1. 请谈一下你对Quartz的理解 思路:根据他解决的什么问题方面去阐述 2. 完成quartz和spring的整合应用 思路:触发时间.任务调度工程 步骤: 1. 创建maven工程,并导入qua ...

  8. 旋转矩阵(Rotate Matrix)的性质分析

    博客转载自:http://www.cnblogs.com/caster99/p/4703033.html 学过矩阵理论或者线性代数的肯定知道正交矩阵(orthogonal matrix)是一个非常好的 ...

  9. c语言学习笔记-变量、变量的命名、变量的赋值和变量的初始化

    在学习了简单的输入输出功能和了解了一些基本的运算符号之后我们可以试着做一个非常简单的计算器. 比如说想计算23+65 输入以下代码就可以了. printf("23+65=%d",2 ...

  10. 一道java笔试题目:Vector和ArrayList的区别

    Vector和ArrayList的区别 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构这些类均在java.util包中本文试图通过 ...