Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.LinkedList;
- import java.util.ListIterator;
- /**
- * http://codeforces.com/problemset/problem/909/D
- * @author Alan Yan
- * Time Limit Exceeded Test Case 15
- */
- public class Task909D {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String s = sc.nextLine();
- LinkedList<Group> list = new LinkedList<Group>();
- char curr = s.charAt(0);
- Group currG = new Group(curr);
- for(int i = 1; i < s.length(); i++) {
- if(curr == s.charAt(i))
- currG.increase();
- else {
- list.add(currG);
- curr = s.charAt(i);
- currG = new Group(curr);
- }
- }
- list.add(currG);
- int ans = 0;
- while(list.size() > 1) {
- int x = Integer.MAX_VALUE;
- for(int i = 0; i < list.size(); i++) {
- x = Math.min(x, (int) Math.ceil((list.get(i).num + 0.0)/(1 + bool(i != 0 && i != list.size() - 1))));
- }
- ans += x;
- ListIterator<Group> one = list.listIterator();
- Group temp = one.next();
- temp.num -= x;
- while(one.hasNext()) {
- temp = one.next();
- temp.num -= 2*x;
- }
- temp.num += x;
- ListIterator<Group> it = list.listIterator();
- while(it.hasNext()) {
- if(it.next().num <= 0)
- it.remove();
- }
- if(list.size() > 1) {
- Group c = it.previous();
- while(!it.hasPrevious()) {
- if(c.color == it.previous().color) {
- it.next().num += c.num;
- }
- it.next(); it.remove();
- c = it.next();
- }
- }
- }
- System.out.print(ans);
- sc.close();
- }
- static int bool(boolean x) {
- if(x) return 1; return 0;
- }
- static class Group{
- char color;
- int num;
- Group(char color){
- this.color = color; num = 1;
- }
- void increase() {
- num++;
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement