PROBLEM LINK:Author: Alei Reyes DIFFICULTY:Cakewalk PREREQUISITES:None PROBLEM:Given a string consisting of characters U,D. In a move you can select a consecutive substring and flip all characters belonging to. (U changes to D and vice versa). What's the minimum number of moves to make all characters equal? EXPLANATION:The first thing that will come up to our minds is that we should split the string from the beginning into a block of consecutive 'U' characters, followed immediately by a block of consecutive 'D' characters, then a block of 'U' characters, then a block of 'D' characters... etc. (Of course if our string starts with D then the letters are the opposite). And flip the D blocks. The number of D blocks = (The number of U blocks) or (the number of U blocks + 1) If our string starts with D then The number of U blocks = (The number of D blocks) or (the number of D blocks + 1) so our answer would be [number of blocks / 2] (of course rounded down). Example:Consider our string was "UUUDDUUUDDDUU" We would split it into : [UUU] , [DD] , [UUU] , [DDD] , [UU] The best solution is to flip [DD] , [DDD] Another Example :Consider our string was "DDDUDDUU" We would split it into : [DDD] , [U] , [DD] , [UU] Here because the number of U blocks is equal to number of D blocks then flipping the blocks of any letter would be optimal. Why is this always correct? If we are looking for a better solution than the mentioned above, then we should flip at least 2 of our targeted blocks at once. But that's impossible because we would affect at least one proper block between them (consisting of characters equal to our final character) and we must return it to its original state again and that's impossible without an additional move. Consider we tried to obtain a better strategy in the first example: That means that we would change all D letters to U in one move. It's only possible by flipping the substring [DDUUUDDD] (or any substring formed by extending this one from left or right), but doing this would change U letters to D and we need to return them back to U, and this will cost us at least one operation. AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 19 Jun '17, 03:34

For what input is this code giving wrong answer? I have tried many cases already.
answered 22 Jun '17, 17:27

@Vijju123 My code is also seems working correct.But not accepted in code chef :(
class Ada_and_crayons {
private static InputReader in = new InputReader(System.in);
public static void main(String[] args)throws Exception
{
int T= in.readInt();
StringBuilder sb = new StringBuilder();
while(T >0)
{
String str = in.readString();
char chr[] = str.toCharArray();
int U_count=0;
int D_count=0;
for(int i =0;i<chr.length;i++)
{
if(chr[i]=='U')
{
U_count++; answered 24 Jun '17, 15:37

What is the problem with my code https://www.codechef.com/viewsolution/14621807 answered 20 Jul '17, 14:27

my code is= include <iostream>using namespace std; int main() {int t; cin>>t; while(t>0) {char s[100]; int i,p=0,c1=0,c2=0; cin>>s; for(i=0;i!='\n';i++) { if(s[i]=='U') { if(p==1) {continue;} else { c1++; p=1; } } else if(s[i]=='D') { if(p==2) {continue; } else { c2++; p=2; } } } if(c1>=c2){cout<<c2<<endl;} else {cout<<c1<<endl;} t; } return 0; } again and again it is showing wrong answer but in my machine it is working fine answered 13 Mar, 19:58

my code is= include <iostream>using namespace std; int main() {int t; cin>>t; while(t>0) {char s[100]; int i,p=0,c1=0,c2=0; cin>>s; for(i=0;i!='\n';i++) { if(s[i]=='U') { if(p==1) {continue;} else { c1++; p=1; } } else if(s[i]=='D') { if(p==2) {continue; } else { c2++; p=2; } } } if(c1>=c2){cout<<c2<<endl;} else {cout<<c1<<endl;} t; } return 0; } again and again it is showing wrong answer but in my machine it is working fine answered 13 Mar, 19:58

my code is= include <iostream>using namespace std; int main() {int t; cin>>t; while(t>0) {char s[100]; int i,p=0,c1=0,c2=0; cin>>s; for(i=0;i!='\n';i++) { if(s[i]=='U') { if(p==1) {continue;} else { c1++; p=1; } } else if(s[i]=='D') { if(p==2) {continue; } else { c2++; p=2; } } } if(c1>=c2){cout<<c2<<endl;} else {cout<<c1<<endl;} t; } return 0; } again and again it is showing wrong answer but in my machine it is working fine answered 13 Mar, 19:58

useless. Even couldn't explain simple thing answered 19 Jun '17, 21:47
4
Well even simple problems need proofs. and proofs are usually harder than the problem. If I asked are prime numbers infinite you would tell yes because it's clear that they are. But can you prove that easily? Proofs are necessary for even simple problems, if this problem seems very easy for you and I am making it more complicated then you can skip them, but just saying that you need to do this without a proof is not ideal (even if the proof is complicated) if you really think it's useless you should say why and what can we fix? not just throw a comment.
(21 Jun '17, 22:42)
1
I agree with @deadwing97 . Its easy to throw tantrums. If you really want something to fix, then suggest something. OR atleast, bring it up POLITELY. You know, never hurts being nice ^^
(21 Jun '17, 23:33)

Useless tutorials are here but important ones are not :(
They will be uploaded in few hours, have some patience please.
At some point of time in past, these tutorials wouldn't have been that useless for you mate.