Sicily 2015 A New Year Gift
2014-11-24 21:37
253 查看
一开始的想法是直接模拟,但是提交之后发现会超时。参考了各位大神的思路,采用二分逼近查找。最终的种类数量肯定在最低值0与最高值sum/m(总珍珠数/组数)之间。每次去中间值进行逼近,知道bot > top为止。采用贪心算法,不用考虑具体实现,每次只要取种类数和每种的个数中的较小值即可。最终对二分逼近的值进行判断,若能组成则输出,否则减一输出
// Problem#: 2015 // Submission#: 3269318 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int m, n; int top, bot, mid; int a[1005]; int sum = 0; while (scanf("%d", &n) && n) { sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } scanf("%d", &m); bot = 0; // 低值 top = sum / m; // 高值 while (bot < top) { // 判断终止条件 mid = (bot + top) / 2; // 二分中值逼近 sum = 0; for (int i = 0; i < n; i++) { sum += min(mid, a[i]); } sum >= mid * m ? bot = mid + 1: top = mid - 1; // 改变bot 或top } sum = 0; for (int i = 0; i < n; i++) { // 判断是否可以组成二分逼近的结果 sum += min(bot, a[i]); } printf("%d\n", sum >= bot * m ? bot : bot - 1); } }
相关文章推荐
- sicily 2015 A New Year Gift
- Sicily 2015 A New Year Gift
- Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
- Good Bye 2015 D. New Year and Ancient Prophecy
- Good BYe 2015 D New Year and Ancient Prophecy
- Good Bye 2015 D. New Year and Ancient Prophecy(dp+lcp)
- 中山大学校队内部选拔赛试题试题2【New Year Gift】--------2015年2月8日
- A New Year Gift
- Codeforces Good Bye 2015 A. New Year and Days
- Codeforences Goodbye 2015 C. New Year and Domino
- Buy RSorder runescape gold 2007 legit with 10% bonus as 2017 New year gift 12.16-1.4
- Codeforences Goodbye 2015 B New Year and Old Property
- Sicily 2015. A New Year Gift
- A New Year Gift
- Codeforces Good Bye 2015 C. New Year and Domino 前缀和
- 【Goodbye2015】Codeforces 611B New Year and Old Property【思维】
- Good Bye 2015 D. New Year and Ancient Prophecy(dp+LCP)
- Good Bye 2015-New Year and Days(看日历找规律)
- CF Good Bye 2015 A- New Year and Days(CF611A)
- sicily 2015. A New Year Gift