USACO Section 1.3 Barn Repair
来源:互联网 发布:淘宝店铺如何关闭 编辑:程序博客网 时间:2024/06/11 09:59
本题又是一道需要用到贪心算法求解的题目,题目的原文如下。
原文
Barn Repair
It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full.
The cows spend the night in stalls that are arranged adjacent to each other in a long line. Some stalls have cows in them; some do not. All stalls are the same width.
Farmer John must quickly erect new boards in front of the stalls, since the doors were lost. His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards. Farmer John wishes to minimize the total length of the boards he must purchase.
Given M (1 <= M <= 50), the maximum number of boards that can be purchased; S (1 <= S <= 200), the total number of stalls; C (1 <= C <= S) the number of cows in the stalls, and the C occupied stall numbers (1 <= stall_number <= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.
Print your answer as the total number of stalls blocked.
PROGRAM NAME: barn1
INPUT FORMAT
Line 1:M, S, and C (space separated)Lines 2-C+1:Each line contains one integer, the number of an occupied stall.SAMPLE INPUT (file barn1.in)
4 50 1834681415161721252627303140414243
OUTPUT FORMAT
A single line with one integer that represents the total number of stalls blocked.SAMPLE OUTPUT (file barn1.out)
25
分析
提交代码
/*ID:PROG: barn1LANG: C++*/#include <fstream>#include <algorithm>#include <vector>#include <string>#include <math.h>#include <map>#include <iostream>using namespace std;int main(){ifstream fin("barn1.in");int M,S,C;fin >> M>> S >> C;vector<int> ocstall(C);for (int i=0;i!=C;i++){fin >> ocstall[i];}sort(ocstall.begin(),ocstall.end());int lenghth = ocstall[ocstall.size()-1] - ocstall[0]+1;vector<int> internal(C-1);for (int i=0;i!=C-1;i++){internal[i] = ocstall[i+1] - ocstall[i] - 1;}sort(internal.begin(),internal.end());for (int i=0;i<M-1;i++){if(internal.size()-1<i)break;lenghth -= internal[internal.size()-1-i];}ofstream fout("barn1.out");fout << lenghth <<endl;return 0;}
提交结果
TASK: barn1LANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 3496 KB] Test 2: TEST OK [0.000 secs, 3496 KB] Test 3: TEST OK [0.000 secs, 3496 KB] Test 4: TEST OK [0.000 secs, 3496 KB] Test 5: TEST OK [0.005 secs, 3496 KB] Test 6: TEST OK [0.005 secs, 3496 KB] Test 7: TEST OK [0.011 secs, 3496 KB] Test 8: TEST OK [0.005 secs, 3496 KB] Test 9: TEST OK [0.008 secs, 3496 KB] Test 10: TEST OK [0.005 secs, 3496 KB]All tests OK.
官方给出的参考答案
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#define MAXSTALL 200int hascow[MAXSTALL];intintcmp(const void *va, const void *vb){return *(int*)vb - *(int*)va;}voidmain(void){ FILE *fin, *fout; int n, m, nstall, ncow, i, j, c, lo, hi, nrun; int run[MAXSTALL]; fin = fopen("barn1.in", "r"); fout = fopen("barn1.out", "w"); assert(fin != NULL && fout != NULL); fscanf(fin, "%d %d %d", &m, &nstall, &ncow); for(i=0; i<ncow; i++) {fscanf(fin, "%d", &c);hascow[c-1] = 1; } n = 0;/* answer: no. of uncovered stalls */ /* count empty stalls on left */ for(i=0; i<nstall && !hascow[i]; i++)n++; lo = i; /* count empty stalls on right */ for(i=nstall-1; i>=0 && !hascow[i]; i--)n++; hi = i+1; /* count runs of empty stalls */ nrun = 0; i = lo; while(i < hi) {while(hascow[i] && i<hi) i++;for(j=i; j<hi && !hascow[j]; j++) ;run[nrun++] = j-i;i = j; } /* sort list of runs */ qsort(run, nrun, sizeof(run[0]), intcmp); /* uncover best m-1 runs */ for(i=0; i<nrun && i<m-1; i++)n += run[i]; fprintf(fout, "%d\n", nstall-n); exit(0);}
THE END
- [USACO]Section 1.3 Barn Repair
- USACO Section 1.3 Barn Repair
- USACO Section 1.3 Barn Repair
- USACO Section 1.3 - Barn Repair
- USACO-Section 1.3 Barn Repair[...]
- USACO Section 1.3 Barn Repair
- USACO Section 1.3.3 Barn Repair
- USACO Section 1.3.2 Barn Repair
- USACO-Section 1.3 Barn Repair(贪心)
- USACO Section 1.3 Barn Repair - 卡了一年的DP...
- USACO Training Section 1.3 Barn Repair 解题报告&AC代码
- USACO 1.3-Barn Repair
- USACO 1.3 Barn Repair
- USACO 1.3 Barn Repair
- USACO--1.3Barn Repair
- USACO 1.3 Barn Repair
- USACO 1.3-Barn Repair
- USCAO-Section 1.3 Barn Repair
- Btrfs文件系统使用说明 .
- 我的苹果5S丢了关机了还能定位找回吗
- [Python]面向对象编程之描述符(Descriptors)
- 求二进制数中1的个数
- UVA 1252 Twenty Questions | dp_状态压缩
- USACO Section 1.3 Barn Repair
- C++ Primer学习 《Strings》
- Java NIO与IO
- Java中的static、final、this、super
- 【SDnoip2014夏令营】【day6】
- 形如Mon Dec 31 00:00:00 CST 2012字符串转换为相应日期Date
- 用java生成条形码,barcode4j得应用
- EXT JS认识EXTJS,第一个EXTJS例子
- UVa 1392 DNA Regions 解题报告(二分)