POJ 1759 Garland
题目链接:https://vjudge.net/problem/POJ-1759
题目大意
有一个长度为N 的数列 H,满足:
- H[1] = A。
- H[N] = B。
- H[i] = (H[i - 1] + H[i + 1]) / 2 - 1,1 < i < N。
- H[i] >= 0,1 <= i <= N。
现已知 A,问 B 最小是多少?
分析
$$
\begin{align*}
\sum_{k = i}^{j} H[k] = H[i] + H[j] - (j - i - 1) + \sum_{k = i + 1}^{j - 1} \frac{H[i - 1] + H[i + 1]}{2} \\
化简后即:H[i + 1] + H[j - 1] = H[i] + H[j] - 2 * (j - i - 1)
\end{align*} \tag{1}
$$
$$
\begin{align*}
H[i] = (i - j + 1) * H[j] - (i - j) * H[j - 1] + (i - j) * (i - j + 1)
\end{align*} \tag{2}
$$
$$
\begin{align*}
H[r - 1] = 2 - r + \frac{H[1]}{r - 1}
\end{align*}
$$
进而可以得到:
$$
\begin{align*}
H[r + 1] = r - \frac{H[1]}{r - 1}
\end{align*}
$$
再将 i = N, j = r + 1 带入(2)可以得到 H[N] 关于 r 的表达式:
$$
\begin{align*}
H[N] = (N - r) *(N - 1 - \frac{H[1]}{r - 1}),H[r - 1] \geq 0, H[r + 1] \geq 0
\end{align*}
$$
求一下导数得到 H[N] 在 H[1] == (r - 1)2 时取到最小值。考虑到此时 r 可能不是整数,因此只要讨论 r 两边的整数即可。
对于 r > N - 1 的情况就不必算了,直接输出 0。
代码如下
1 #include <cmath> 2 #include <ctime> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <queue> 10 #include <map> 11 #include <set> 12 #include <algorithm> 13 #include <cctype> 14 #include <stack> 15 #include <deque> 16 #include <list> 17 #include <sstream> 18 using namespace std; 19 20 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 21 #define Rep(i,n) for (int i = 0; i < (n); ++i) 22 #define For(i,s,t) for (int i = (s); i <= (t); ++i) 23 #define rFor(i,t,s) for (int i = (t); i >= (s); --i) 24 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i) 25 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i) 26 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) 27 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) 28 29 #define pr(x) cout << #x << " = " << x << " " 30 #define prln(x) cout << #x << " = " << x << endl 31 32 #define LOWBIT(x) ((x)&(-x)) 33 34 #define ALL(x) x.begin(),x.end() 35 #define INS(x) inserter(x,x.begin()) 36 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end()) 37 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 38 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower); 39 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); 40 41 #define ms0(a) memset(a,0,sizeof(a)) 42 #define msI(a) memset(a,inf,sizeof(a)) 43 #define msM(a) memset(a,-1,sizeof(a)) 44 45 #define MP make_pair 46 #define PB push_back 47 #define ft first 48 #define sd second 49 50 template<typename T1, typename T2> 51 istream &operator>>(istream &in, pair<T1, T2> &p) { 52 in >> p.first >> p.second; 53 return in; 54 } 55 56 template<typename T> 57 istream &operator>>(istream &in, vector<T> &v) { 58 for (auto &x: v) 59 in >> x; 60 return in; 61 } 62 63 template<typename T1, typename T2> 64 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { 65 out << "[" << p.first << ", " << p.second << "]" << "\n"; 66 return out; 67 } 68 69 inline int gc(){ 70 static const int BUF = 1e7; 71 static char buf[BUF], *bg = buf + BUF, *ed = bg; 72 73 if(bg == ed) fread(bg = buf, 1, BUF, stdin); 74 return *bg++; 75 } 76 77 inline int ri(){ 78 int x = 0, f = 1, c = gc(); 79 for(; c<48||c>57; f = c=='-'?-1:f, c=gc()); 80 for(; c>47&&c<58; x = x*10 + c - 48, c=gc()); 81 return x*f; 82 } 83 84 template<class T> 85 inline string toString(T x) { 86 ostringstream sout; 87 sout << x; 88 return sout.str(); 89 } 90 91 inline int toInt(string s) { 92 int v; 93 istringstream sin(s); 94 sin >> v; 95 return v; 96 } 97 98 //min <= aim <= max 99 template<typename T> 100 inline bool BETWEEN(const T aim, const T min, const T max) { 101 return min <= aim && aim <= max; 102 } 103 104 typedef long long LL; 105 typedef unsigned long long uLL; 106 typedef pair< double, double > PDD; 107 typedef pair< int, int > PII; 108 typedef pair< int, PII > PIPII; 109 typedef pair< string, int > PSI; 110 typedef pair< int, PSI > PIPSI; 111 typedef set< int > SI; 112 typedef set< PII > SPII; 113 typedef vector< int > VI; 114 typedef vector< double > VD; 115 typedef vector< VI > VVI; 116 typedef vector< SI > VSI; 117 typedef vector< PII > VPII; 118 typedef map< int, int > MII; 119 typedef map< int, string > MIS; 120 typedef map< int, PII > MIPII; 121 typedef map< PII, int > MPIII; 122 typedef map< string, int > MSI; 123 typedef map< string, string > MSS; 124 typedef map< PII, string > MPIIS; 125 typedef map< PII, PII > MPIIPII; 126 typedef multimap< int, int > MMII; 127 typedef multimap< string, int > MMSI; 128 //typedef unordered_map< int, int > uMII; 129 typedef pair< LL, LL > PLL; 130 typedef vector< LL > VL; 131 typedef vector< VL > VVL; 132 typedef priority_queue< int > PQIMax; 133 typedef priority_queue< int, VI, greater< int > > PQIMin; 134 const double EPS = 1e-8; 135 const LL inf = 0x3fffffff; 136 const LL infLL = 0x3fffffffffffffffLL; 137 const LL mod = 1e9 + 7; 138 const int maxN = 1e5 + 7; 139 const LL ONE = 1; 140 const LL evenBits = 0xaaaaaaaaaaaaaaaa; 141 const LL oddBits = 0x5555555555555555; 142 143 int N, r; 144 double A, B = inf; 145 146 int main(){ 147 //freopen("MyOutput.txt","w",stdout); 148 //freopen("input.txt","r",stdin); 149 //INIT(); 150 cin >> N >> A; 151 r = sqrt(A) + 1 + EPS; 152 if(r > N - 1) return printf("0.00\n");; 153 154 if((r - 2) * (r - 1) <= A && r * (r - 1) >= A) B = min(B, (N - r) * (N - 1 - 1.0 * A / (r - 1))); 155 if(r * (r - 1) <= A && r * (r + 1) >= A) B = min(B, (N - r - 1) * (N - 1 - 1.0 * A / r)); 156 printf("%.2f\n", B + EPS); 157 return 0; 158 }
View Code
转载于:https://www.cnblogs.com/zaq19970105/p/11165676.html
POJ 1759 Garland相关推荐
- 《挑战程序设计竞赛(第2版)》习题册攻略
本项目来源于GitHub 链接: 项目GitHub链接 1 前言 项目为<挑战程序设计竞赛(第2版)>习题册攻略,已完结.可配合书籍或笔记,系统学习算法. 题量:约200道,代码注释内含详 ...
- POJ前面的题目算法思路【转】
1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...
- POJ 超详细分类
POJ 各题算法 1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 ...
- POJ的题目分类(两个版本)
版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...
- 【算法总结】二分搜索
一. STL函数 lower_bound() 试图在已排序的 [first, last) 中寻找元素 value.返回一个迭代器,指向第一个"不小于 value"的元素,如果 va ...
- (精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目
POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: ...
- poj题目详细分类及算法推荐题目
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
- ACM POJ 题目分类(完整整理版本)
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
- POJ ZOJ题目分类
POJ,ZOJ题目分类(多篇整合版,分类很细致,全面) 标签: 题目分类POJ整理 2015-04-18 14:44 1672人阅读 评论(0) 收藏 举报 本文章已收录于: 分类: ACM资料(5) ...
最新文章
- 打开快手,体验流畅的单目三维手势技术
- .net C# 关于使用npoi导入excel 所遇到的问题PublicKeyToken=0df73ec7942b34e1
- Linux脚本实战之猜数字
- Linux下如何创建loop device
- 重力加速度换算_中考物理重难点汇总——公式换算大全
- 20180802总结
- 2061:【例1.2】梯形面积【入门题】
- java int 指针_如何在Java中使用指针?
- 怎样用python把数据分开_python使用pandas实现数据分割实例代码
- svn服务器搭建之备份3
- Mongodb语法学习:查询
- proto3文件定义Demo-用户表单条、多条、所有、编辑
- 苹果6s强制删除id锁_苹果ID锁安全神话破灭!2分钟就能解锁
- 专访Nick McKeown:网络领域的游戏颠覆者
- Docker实时查看日志命令
- 电大本科计算机应用基础模块6,国家开放大学计算机应用基础模块3形考答案.xls...
- python3单例模式_python3中的单例模式Singleton
- L1-030. 一帮一
- 超实用的 IPTV 管理工具,xTeVe 助你定制专属电视频道。
- android仿支付宝弹窗,AlipayPassDialog Android 仿支付宝密码键盘弹框,可以自定义样式 Dialog,调节字体颜色大小内容 @codeKK Android开源站...
热门文章
- 【android】Airtest IDE实现多设备管理以及自动装包
- 解决service iptables save出错please try to use systemctl.
- Linux NFS存储服务部署
- 步步为营-93-MVC+EF简单实例
- 阻塞IO, 非阻塞IO, 同步IO,异步IO
- 160719、Spring + Dubbo + zookeeper (linux) 框架搭建
- 菜鸟的MySQL学习笔记(一)
- python pandas series_Python数据分析-pandas之Series
- 如何修改Vue和springboot的默认端口号
- SpringBoot登录拦截器