博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3427 Ecology tax(我的水题之路——不同的理解,不同的AC)
阅读量:4068 次
发布时间:2019-05-25

本文共 2894 字,大约阅读时间需要 9 分钟。

Ecology tax
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3677   Accepted: 1901

Description

In a big and rich on natural resources country, the government started a campaign to control deforestation. In fact the government is not too interested in how many trees get fallen, but rather how effectively the wood is utilized. So a law was passed which requires every logging company to pay amount of money in proportion to amount of wood that it wastes during operation.

A felling quota on some territory was allotted to a company in this country. Company lorries may only transport logs of exactly L meters long. So when a tree gets sawed into logs, the remainder is wasted.

Trees in this country grow exactly 1 meter per year, so the company may decrease the amount of tax to be paid by simply waiting for some years. Your task is to determine the number of years needed to achieve smallest possible tax. If there is more than one answer, find minimal (earliest) one.

Input

Input file contains number of trees N, length of log L, followed by integers i1 i2 ... iN — heights of each tree.

Constraints

1 ≤ N ≤ 30000, 1 ≤ Lik ≤ 30000

Output

Output file must contain single integer — number of years to wait.

Sample Input

Sample Input 13 1 10 15 11Sample Input 23 25 3 6

Sample Output

Sample Output 10Sample Output 21

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

Source

, Far-Eastern Subregio
说有一家公司要开采木头,有N棵树,长度不同,公司的卡车的长度是L,所以必须把木头砍成单位为L的几段,多余部分丢弃。已经森林里树木每年均生长单位1,问,过多少年砍伐树木,使得丢弃的树木量最少。
这道题,我混乱了,看到的数据量范围是3W,如果直接暴力,3W棵树,3W米,就会有9*10^8的计算量,必定要超时。思考了很久有没有数论相关的解题方案发现没有。所以只好试着暴力求解,就有了本题的
“理解思路一”:
公司不对森林进行任何砍伐,任由树木生长,等到损失量最小的时候,统一砍伐。
这样的话,我直接用暴力,计算出[0, L - 1]年后,每一年的树木在生长后的损失量,然后求到最小值,输出。强大的是,居然没有WA、没有TLE、没有RE,直接AC了。- -
看到这个结果我很失落,不过我还是想找一个数论相关的伟大算法,结果就搜到一个更水的
“理解思路二”:
公司看到森林这个样子很不爽,于是每年把刚刚好合适的树木砍掉,第二年再把第二年适合的树木砍掉……直到所有的N棵树木被看完,就输出结果。
简单模拟,代码更短- -,竟然AC 了。
好吧,综上,我对这道题很无奈。。。。
代码一(解题思路一  1AC):
#include 
#include
int arr[31000];int main(void){ int n, log; int i, j, mingy; long long min, sum; scanf("%d%d", &n, &log); for (i = 0; i < n; i++){ scanf("%d", &arr[i]); arr[i] %= log; } for (i = 0; i < log; i++){ for (sum = j = 0; j < n; j++){ sum += (arr[j] + i) % log; } if (!i){ min = sum; mingy = i; } if (min > sum){ min = sum; mingy = i; } } printf("%d\n", mingy); return 0;}
代码二(解题思路二,别人的代码,1AC):
#include
using namespace std;int max(int a, int b){ return a > b ? a : b;}int main(){ int tree, len; while(cin >> tree >> len){ int hei, ans = 0; while(tree --){ cin >> hei; if(hei % len == 0) continue; ans = max(ans, len - hei % len); } cout << ans << endl; } return 0;}

转载地址:http://hloji.baihongyu.com/

你可能感兴趣的文章
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
23. Merge k Sorted Lists (Divide and conquer, Linked List) 以及java匿名内部类
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>
剑指_栈的压入弹出序列
查看>>
剑指_复杂链表的复制
查看>>
服务器普通用户(非管理员账户)在自己目录下安装TensorFlow
查看>>
星环后台研发实习面经
查看>>
大数相乘不能用自带大数类型
查看>>
字节跳动后端开发一面
查看>>
CentOS Tensorflow 基础环境配置
查看>>
centOS7安装FTP
查看>>