lightoj1048 Conquering Keokradong
2015-11-24 19:58
239 查看
这是一个很不错的二分,,,可是wa了半天没做对,主要是二分最大单个区间的值,,,mid,
当a[i] > mid的时候,low++,当cnt > k时,low++;cnt <= k时,high = mid;
然后就是打印比较容易挂掉;注意下就好了。
当a[i] > mid的时候,low++,当cnt > k时,low++;cnt <= k时,high = mid;
然后就是打印比较容易挂掉;注意下就好了。
// #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <limits.h> // #define DEBUG #ifdef DEBUG #define debug(...) printf( __VA_ARGS__ ) #else #define debug(...) #endif #define MEM(x,y) memset(x, y,sizeof x) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int a[1010]; int n, k; bool check(int x){ // cout << "x = " << x << endl; int sum = 0,cnt = 0; for (int i = 0;i <= n;++i){ sum += a[i]; if (sum > x){//sum = 0 || sum > 0; cnt++; sum -= a[i]; // printf("sum = %d\t",sum); if (sum <= 0) return false;//a[i] > x;->low = mid + 1; sum = a[i]; } } // cout << "cnt = " << cnt << endl; return cnt <= k;//cnt > k,->low = mid + 1; } int solve(int low,int high){ int mid; while(low < high){ mid = (low + high) / 2; if (check(mid)) high = mid;//保存结果,,,cnt <= k; else low = mid + 1; } return high; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int t, icase = 0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&k); int low = 0,high = 0; for (int i = 0;i <= n;++i){ scanf("%d",&a[i]); low = max(low, a[i]); high += a[i]; } int ans = solve(low,high); printf("Case %d: %d\n",++icase,ans); int tot,i,j; for (i = 0,j = 0,tot = 0;i <= n;++i){ tot += a[i]; if (tot > ans || k - j > n - i){ tot -= a[i]; printf("%d\n",tot); tot = a[i]; j++; } } printf("%d\n",tot); } return 0; }
相关文章推荐
- UI第十一天:UITableView ⾼级
- MIUI6&7桌面角标开源代码简介
- TextView设置android:ellipsize=marquee不滚动
- 读书笔记--Android布局和UI组件中的布局管理器
- UIScrollView 手势缩放
- UGUI 文本下划线实现
- Qt5.5中使用serialport类使得Ui界面很卡
- Handler、Message、MessageQueue、Looper调用过程源码浅析
- 10.26~11.23刷题(shui)记录
- System.Web.UI.ViewStateException: Invalid viewstate 解决方法
- 实现UITableView上View的上拉隐藏下拉出现效果(1)(category)
- invalid use of incomplete type 'class-Ui-Widge't的解决办法
- Tui-x 自适应屏幕 (转) ----- 6
- Tui-x 运行demo和部署项目 ( 转 ) ----- 5
- Tui-x 资源管理( 转 ) ----- 4
- Tui-x 命名规则( 转 ) ----- 3
- Tui-x 基础使用 ( 转 ) ----- 2
- Tui-x简介 ( 转 ) ---- 1
- UIScrollView的属性总结
- easyui-datagrid---动态创建表头和加载数据