题目地址

https://loj.ac/problem/2764

题解

真的想不到二分...不看tag的话...

考虑二分答案转化为判定问题,那么问题就变成了能不能组合出x个JOI/IOI,考虑贪心判定,倒着做,统计I的个数cnt,已组OI的个数tot,以及JOI/IOI个数ans。对于J显然直接找一个OI组成答案。对于O显然直接找I。对于I需要贪心考虑,假设目前的cnt+tot+ans>=x那么就组答案,否则做OI里面的那个I,让cnt++。(贪心考虑只需要造x个OI,造多了会浪费)

#include <bits/stdc++.h>
using namespace std; #define ll long long
const int N = 1000010; int n;
char s[N]; bool check(int x) {
int ans = 0, tot = 0, cnt = 0;
for(int i = n; i; --i) {
if(x == ans) return 1;
if(s[i] == 'J') {
if(tot) --tot, ++ans;
continue;
}
if(s[i] == 'O') {
if(cnt) --cnt, ++tot;
continue;
}
if(s[i] == 'I') {
if(ans + tot + cnt < x) cnt++;
else {
if(tot) --tot, ++ans;
}
}
}
if(x == ans) return 1;
return 0;
} int main() {
scanf("%d%s", &n, s + 1);
int l = 0, r = n, ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if(check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
}

最新文章

  1. easyui Datagrid查询报错Uncaught TypeError:Cannot read property &#39;length&#39; of undefined
  2. Python与Hack之Zip文件口令破解
  3. linux ps指令
  4. JS初识(着重讲解Date函数)
  5. js之createTextRange方法
  6. HDU 4717The Moving Points warmup2 1002题(三分)
  7. Firemonkey的旁门左道[四]
  8. Effective Java 学习笔记之第七条——避免使用终结(finalizer)方法
  9. U盘安装CentOS7
  10. Metadata是.NET平台的核心灵魂--(转载)
  11. MVC写在Model文件夹下,登录注册等页面定义的变量规则,不会被更新实体模型删除
  12. Java线程:线程安全类和Callable与Future(有返回值的线程)
  13. iOS-swift-协议和拓展
  14. 阿里云API网关(7)开发指南-API参考
  15. Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路
  16. php中include和require的区别(整理)
  17. ERP项目应该由谁来主导?
  18. 004.Ceph块设备基础使用
  19. vi分屏指令
  20. IIC通讯协议(非原创,转载他人,用于学习)

热门文章

  1. kexue shangwang
  2. idea关闭sonarLint自动扫描
  3. PHP设计模式 - 模板方法模式
  4. c++基础(五)——关联容器
  5. leetcode 罗马数字和数字的互相转换
  6. Golang_互斥锁
  7. shell中if条件字符串、数字比对,[[ ]]和[ ]区别
  8. WPF内嵌网页的两种方式
  9. Asp.netMVC中Ajax.BeginForm上传文件
  10. 正则表达式&quot;(^|&amp;)&quot; ,什么意思?