得物笔试AK
1、存在长度为x的回文子串
直接暴力即可
2、n栋楼总高度为m,楼高至少为1,相邻楼之间高度差不超过1,求第x栋楼的最大高度。
采用的是二分法,设第x栋楼高为h,则要想达到这个高度的最小的总高度是可以求出的,再去判断它和m的相对大小。
#include <iostream> using namespace std; int main(void){ int n, m, x; cin >> n >> m >> x; int left = 1, right = m; auto minHeight = [&](int h){ long long minHeights = 0; if (h <= x){ minHeights += 1LL*(h+1)*h/2 + (x-h); }else{ minHeights += 1LL*(h-x+1+h)*x/2; } if (h-1 <= n-x){ minHeights += 1LL*(h-1)*h/2 + (n-x-h+1); }else{ minHeights += 1LL*(2*h-n+x-1)*(n-x)/2; } return minHeights; } while (left <= right){ int mid = (left + right)/2; int minHeights = minHeight(mid); if (minHeights == m){ cout << mid << endl; return 0; }else if (minHeights < m){ left = mid + 1; }else{ right = mid - 1; } } cout << right << endl; return 0; }