Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.LinkedList;
  3. import java.util.ListIterator;
  4. /**
  5. * http://codeforces.com/problemset/problem/909/D
  6. * @author Alan Yan
  7. * Time Limit Exceeded Test Case 15
  8. */
  9. public class Task909D {
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12. String s = sc.nextLine();
  13. LinkedList<Group> list = new LinkedList<Group>();
  14. char curr = s.charAt(0);
  15. Group currG = new Group(curr);
  16. for(int i = 1; i < s.length(); i++) {
  17. if(curr == s.charAt(i))
  18. currG.increase();
  19. else {
  20. list.add(currG);
  21. curr = s.charAt(i);
  22. currG = new Group(curr);
  23. }
  24. }
  25. list.add(currG);
  26.  
  27. int ans = 0;
  28. while(list.size() > 1) {
  29. int x = Integer.MAX_VALUE;
  30. for(int i = 0; i < list.size(); i++) {
  31. x = Math.min(x, (int) Math.ceil((list.get(i).num + 0.0)/(1 + bool(i != 0 && i != list.size() - 1))));
  32. }
  33. ans += x;
  34. ListIterator<Group> one = list.listIterator();
  35.  
  36. Group temp = one.next();
  37. temp.num -= x;
  38. while(one.hasNext()) {
  39. temp = one.next();
  40. temp.num -= 2*x;
  41. }
  42. temp.num += x;
  43. ListIterator<Group> it = list.listIterator();
  44. while(it.hasNext()) {
  45. if(it.next().num <= 0)
  46. it.remove();
  47. }
  48. if(list.size() > 1) {
  49. Group c = it.previous();
  50. while(!it.hasPrevious()) {
  51. if(c.color == it.previous().color) {
  52. it.next().num += c.num;
  53. }
  54. it.next(); it.remove();
  55. c = it.next();
  56. }
  57. }
  58. }
  59. System.out.print(ans);
  60. sc.close();
  61. }
  62. static int bool(boolean x) {
  63. if(x) return 1; return 0;
  64. }
  65. static class Group{
  66. char color;
  67. int num;
  68. Group(char color){
  69. this.color = color; num = 1;
  70. }
  71. void increase() {
  72. num++;
  73. }
  74. };
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement