Hdu3275 Light is very good, and the line segment tree is rich in harvest.

Source: Internet
Author: User

Light
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 707 Accepted Submission (s): 224


Problem Description
Felicia was given a long string of fancy lanterns, but not all of them are on to light the night. felicia felt bad about that and he wants to light up all the lanterns. there is a kind of magic switch which can change the states of k continuous lanterns. once you choose k continuous lanterns and install a switch on them, the states of all k continuous lanterns can be changed together (on-> off or off-> on ), but you cannot choose some ones be changed and some ones not be changed.
Felicia wants to buy as few switches as possible so that he can install them to turn on all the lanterns. Please notice that each switch must control exactly k continuous lanterns.
 

Input
The input consists of multiple test cases. the first line of each case contains integers n (0 <n <= 100000), which is the length of that string of fancy lanterns, and k (0 <= k <= n), which is the number of continuous lanterns that a switch will control. the next line consists of a string of "01" with length n. "1" means that lantern is on and "0" means off. your job is turn all the "0" to "1 ".
The last test case is followed by a line containing two zeros.
 

Output
If you cannot finish this job, output "-1" or you shocould output the minimum number switches that you shoshould buy to turn on all the lanterns.
 

Sample Input
4 2
0000
4 2
1001
4 2
0110
4 2
0111
0 0
 

Sample Output
2
1
3
-1
Question:

Input n k
And n 0 or 1
If you convert all strings to 1, each step will change all k strings starting from pos to the opposite state. At least how many steps are required. If you cannot change all strings to 1, enter-1.


Idea: write several groups of data and you will find that the number from left to right changes from 0 to 1 to 1. This is the minimum step.


The key is how to quickly find the zero position. At this time, we need to use the line segment tree to mark whether there is zero in a segment.

[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Struct haha
{
Int num1;
Int num0;
Int flag;
Int left;
Int right;
} Node [100000*4];
Char s [100000 + 100];
Void build (int nd, int left, int right)
{
Node [nd]. left = left;
Node [nd]. right = right;
Node [nd]. flag = 0;
If (node [nd]. left = node [nd]. right)
{
If (s [node [nd]. left] = '1 ')
{
Node [nd]. num0 = 0;
Node [nd]. num1 = 1;
}
Else
{
Node [nd]. num1 = 0;
Node [nd]. num0 = 1;
}
Return;
}
Int mid = (left + right)/2;
Build (nd * 2, left, mid );
Build (nd * 2 + 1, mid + 1, right );
Node [nd]. num0 = node [nd * 2]. num0 + node [nd * 2 + 1]. num0;
Node [nd]. num1 = node [nd * 2]. num1 + node [nd * 2 + 1]. num1;
}
Void change (int nd)
{
Int temp;
If (node [nd]. left = node [nd]. right) return;
Temp = node [nd * 2]. num0; node [nd * 2]. num0 = node [nd * 2]. num1; node [nd * 2]. num1 = temp;
Temp = node [nd * 2 + 1]. num0; node [nd * 2 + 1]. num0 = node [nd * 2 + 1]. num1; node [nd * 2 + 1]. num1 = temp;
<Span style = "color: # FF0000;"> // node [nd]. flag = 0; node [nd * 2]. flag = 1; node [nd * 2 + 1]. flag = 1; // This is absolutely incorrect because I have made a mistake for 10 times.
Node [nd]. flag =! Node [nd]. flag; node [nd * 2]. flag =! Node [nd * 2]. flag; // you should write this specification later.
Node [nd * 2 + 1]. flag =! Node [nd * 2 + 1]. flag; // because you cannot ensure that a child node can mark 2 sides without being marked. </span>
 
}
Int query (int nd)
{
If (node [nd]. left = node [nd]. right) return node [nd]. left; // = A Brute Force Error
If (node [nd]. flag) change (nd );
Int pos =-1;
If (node [nd * 2]. num0) pos = query (nd * 2 );
Else
If (node [nd * 2 + 1]. num0) pos = query (nd * 2 + 1 );
Node [nd]. num0 = node [nd * 2]. num0 + node [nd * 2 + 1]. num0;
Node [nd]. num1 = node [nd * 2]. num1 + node [nd * 2 + 1]. num1;
Return pos;
}
Void update (int nd, int left, int right)
{
Int I, j, mid;
If (node [nd]. flag) change (nd );
If (node [nd]. left = left & node [nd]. right = right)
{
Int temp;
Temp = node [nd]. num0; node [nd]. num0 = node [nd]. num1; node [nd]. num1 = temp;
Node [nd]. flag = 1;
Return;
}

Mid = (node [nd]. left + node [nd]. right)/2;
If (right <= mid) update (nd * 2, left, right );
Else if (left> mid) update (nd * 2 + 1, left, right );
Else
{
Update (nd * 2, left, mid );
Update (nd * 2 + 1, mid + 1, right );
}
Node [nd]. num0 = node [nd * 2]. num0 + node [nd * 2 + 1]. num0;
Node [nd]. num1 = node [nd * 2]. num1 + node [nd * 2 + 1]. num1;
Return;
 
}
Int main ()
{
Int n, k, I, j, flag, left, right, ans, num, len;
 
While (scanf ("% d", & n, & k )! = EOF)
{
Flag = 0; ans = 0; num = 0;
If (! N &&! K) break;
Scanf ("% s", s + 1); // as long as the input does not need to be converted into an int array, otherwise it will time out, so do not need unnecessary operations
Len = strlen (s + 1 );
Build (1, 1, len );
For (I = 1; I <= len; I ++)
If (s [I] = '0') {flag = 1; break ;}
If (k = 0)
{
If (node [1]. num1 = len) printf ("0 \ n ");
Else
Printf ("-1 \ n ");
Continue;
}
If (flag = 0) {printf ("0 \ n"); continue ;}
Int pos; // The leftmost 0 position of the record
Flag = 0;
While (pos = query (1 ))
{
If (pos =-1) break;
Left = pos; right = pos + k-1;
If (right> len) {printf ("-1 \ n"); flag = 1; break ;}
Update (1, left, right );
Ans ++;
}
If (flag) continue;
Else printf ("% d \ n", ans );
}
Return 0;
}


Harvest: some useless small operations can also cause timeout
In addition, the red fonts are very important and the standards will be written in that way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.