Monday, June 30, 2014

TopCoder High School SRM 30 Level one - 250 pt - How Many Birthdays solution

TopCoder High School SRM 30 Level one - 250 pt - How Many Birthdays:

Problem Statement

     You have several friends and you want to know which of them are celebrating their birthdays today. You are given a String[] friends, each element of which is formatted as "NAME DAY MONTH" (quotes for clarity), where NAME is a friend's name, and DAY and MONTH are integers representing the day and month, respectively, of that friend's birthday. Today's date is given in the String today, formatted as "DAY MONTH" (quotes for clarity). Return a String[] containing the names of all your friends who have birthdays today, sorted in alphabetical order. If none of your friends have a birthday today, return an empty String[].

Definition

    
Class: HowManyBirthdays
Method: getList
Parameters: vector <string>, string
Returns: vector <string>
Method signature: vector <string> getList(vector <string> friends, string today)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Notes

- In this problem, all months have exactly 31 days.

Constraints

- friends will contain between 1 and 50 elements, inclusive.
- Each element of friends will be formatted as "NAME DAY MONTH" (quotes for clarity).
- today will be formatted as "DAY MONTH" (quotes for clarity).
- In each element of friends, NAME will contain between 1 and 20 lowercase letters ('a'-'z'), inclusive.
- Each DAY in friends and today will be an integer between 1 and 31, inclusive, with no leading zeroes.
- Each MONTH in friends and today will be an integer between 1 and 12, inclusive, with no leading zeroes.
- No two elements of friends will contain the same NAMEs.

Examples

0)
    
{"igor 13 6"}
"14 6"
Returns: { }
You have only one friend and he celebrated his birthday yesterday. So, your method should return an empty String[].
1)
    
{"sasha 12 5"}
"12 5"
Returns: {"sasha" }
sasha celebrates his birthday today, so your method should return {"sasha"}.
2)
    
{"larisa 27 3", "kira 7 4", "max 12 3", "kirill 27 3"}
"27 3"
Returns: {"kirill", "larisa" }
3)
    
{"zoro 31 2", "pokemon 16 12", "spiderman 31 12", "dragon 13 7", "elf 31 12"}
"31 12"
Returns: {"elf", "spiderman" }
4)
    
{"ann 6 6", "tanya 6 6", "gudi 6 6", "ruslik 6 6", "alla 6 6", "serge 10 10"}
"6 6"
Returns: {"alla", "ann", "gudi", "ruslik", "tanya" }
5)
    
{"ahdjakdd 31 12", "fhdfjha 1 1", "wefhjks 13 6", "fkajahsdaaajj 6 7"}
"5 12"
Returns: { }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.  

TopCoder High School SRM 30 Level one - 250 pt - How Many Birthdays editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm30

TopCoder High School SRM 30 Level one - 250 pt - How Many Birthdays solution: 


#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

class HowManyBirthdays {
    public: vector <string> getList(vector <string> f, string t) {
        vector <string> a;      
        for(int i=0; i<f.size(); i++) {
            stringstream b(f[i]);
            string c, d, e;
            b>>c>>d>>e;
            if(d+' '+e==t) a.push_back(c);
        }
        sort(a.begin(), a.end());
        return a;
    }
};


tomekkulczynski's solution to HowManyBirthdays : http://community.topcoder.com/tc?module=HSProblemSolution&cr=14886245&rd=10654&pm=7429


#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <iostream>
using namespace std;

class HowManyBirthdays 
{
  public:
  vector <string> getList(vector <string> s, string t) 
  {
    vector<string> ret;
    int i,j;
    for(i=0;i<s.size();i++)
    {
      for(j=0;s[i][j]!=' ';j++);
      if(s[i].substr(j+1)==t) ret.push_back(s[i].substr(0,j));
    }
    sort(ret.begin(),ret.end());
    return ret;
  }
};

TopCoder High School SRM 29 Level one - 250 pt - Reverse Sums solution

TopCoder High School SRM 29 Level one - 250 pt - Reverse Sums statement: http://community.topcoder.com/stat?c=problem_statement&pm=6557&rd=10653

TopCoder High School SRM 29 Level one - 250 pt - Reverse Sums editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm29

TopCoder High School SRM 29 Level one - 250 pt - Reverse Sums solution:

#include <iostream>
using namespace std;

class ReverseSums {
    public: int getSum(int n) {
        int m=0, k=0;
        m=0, k=n;
        while(n>0) {
            m=m*10+n%10;
            n/=10;
        }
        return m+k;
    }
};


TopCoder High School SRM 28 Level one - 250 pt - Swapping Marbles solution

TopCoder High School SRM 28 Level one - 250 pt - Swapping Marbles statement: http://community.topcoder.com/stat?c=problem_statement&pm=6882&rd=10652

TopCoder High School SRM 28 Level one - 250 pt - Swapping Marbles editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm28

TopCoder High School SRM 28 Level one - 250 pt - Swapping Marbles solution: 


#include <iostream>
using namespace std; 

class SwappingMarbles {
    public: int swaptions(string c1, string c2) {
        int cnt=0;
        for(int i=1; i<c1.length()-1; i++) {
            for(int j=1; j<c2.length()-1; j++) {
                if(c1[i-1]==c1[i+1] && c1[i-1]==c2[j] && c1[i]!=c2[j] && c2[j-1]==c2[j+1] && c2[j-1]==c1[i]) cnt++;
            }
        }
        return cnt;
    }
};

TopCoder High School SRM 27 Level one - 250 pt - Brick Mystery solution

TopCoder High School SRM 27 Level one - 250 pt - Brick Mystery statement: http://community.topcoder.com/stat?c=problem_statement&pm=7331&rd=10651

TopCoder High School SRM 27 Level one - 250 pt - Brick Mystery editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm27

TopCoder High School SRM 27 Level one - 250 pt - Brick Mystery solution: 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class BrickMystery {
    public: vector <string> createPattern(string m) {
        vector <string> a;      
        a.push_back("x.......x");
        for(int i=0; i<m.length(); i++) {
            string b="";
            for(int j=0; j<7; j++) {
                if((m[i]>>j)&1) b+='x';
                else b+='.';
            }
            reverse(b.begin(), b.end());
            a.push_back('x'+b+'x');
        }
        a.push_back("x.......x");
        return a;
    }
 };

note: to learn about conversion of decimal numbers to binary numbers: http://garakchy.blogspot.com.tr/2014/06/a-tutorial-on-binary-numbers-conversion.html

reverse() string is of <algorithm> c++ : http://stackoverflow.com/a/198210/2948746

A tutorial on Binary Numbers, Conversion of Decimal Numbers to Binary Numbers.

part of  "A TUTORIAL ON BINARY NUMBERS"

Decimal to Binary

Converting from decimal to binary notation is slightly more difficult conceptually, but can easily be done once you know how through the use of algorithms. Begin by thinking of a few examples. We can easily see that the number 3= 2+1. and that this is equivalent to (1*2^1)+(1*2^0). This translates into putting a "1" in the 2^1 column and a "1" in the 2^0 column, to get "11". Almost as intuitive is the number 5: it is obviously 4+1, which is the same as saying [(2*2) +1], or 2^2+1. This can also be written as [(1*2^2)+(1*2^0)]. Looking at this in columns,
        2^2 | 2^1 | 2^0
         1     0     1
or 101.

What we're doing here is finding the largest power of two within the number (2^2=4 is the largest power of 2 in 5), subtracting that from the number (5-4=1), and finding the largest power of 2 in the remainder (2^0=1 is the largest power of 2 in 1). Then we just put this into columns. This process continues until we have a remainder of 0. Let's take a look at how it works. We know that:
   2^0=1
   2^1=2
   2^2=4
   2^3=8
   2^4=16
   2^5=32
   2^6=64
   2^7=128
and so on. To convert the decimal number 75 to binary, we would find the largest power of 2 less than 75, which is 64. Thus, we would put a 1 in the 2^6 column, and subtract 64 from 75, giving us 11. The largest power of 2 in 11 is 8, or 2^3. Put 1 in the 2^3 column, and 0 in 2^4 and 2^5. Subtract 8 from 11 to get 3. Put 1 in the 2^1 column, 0 in 2^2, and subtract 2 from 3. We're left with 1, which goes in 2^0, and we subtract one to get zero. Thus, our number is 1001011.

Making this algorithm a bit more formal gives us:
  1. Let D=number we wish to convert from decimal to binary
  2. Repeat until D=0
    • a. Find the largest power of two in D. Let this equal P.
    • b. Put a 1 in binary column P.
    • c. Subtract P from D.
  3. Put zeros in all columns which don't have ones.
This algorithm is a bit awkward. Particularly step 3, "filling in the zeros." Therefore, we should rewrite it such that we ascertain the value of each column individually, putting in 0's and 1's as we go:

  1. Let D= the number we wish to convert from decimal to binary
  2. Find P, such that 2^P is the largest power of two smaller than D.
  3. Repeat until P<0
    • If 2^P<=D then
      • put 1 into column P
      • subtract 2^P from D
    • Else
      • put 0 into column P
    • End if
    • Subtract 1 from P
Now that we have an algorithm, we can use it to convert numbers from decimal to binary relatively painlessly. Let's try the number D=55.
  • Our first step is to find P. We know that 2^4=16, 2^5=32, and 2^6=64. Therefore, P=5.
  • 2^5<=55, so we put a 1 in the 2^5 column: 1-----.
  • Subtracting 55-32 leaves us with 23. Subtracting 1 from P gives us 4.
  • Following step 3 again, 2^4<=23, so we put a 1 in the 2^4 column: 11----.
  • Next, subtract 16 from 23, to get 7. Subtract 1 from P gives us 3.
  • 2^3>7, so we put a 0 in the 2^3 column: 110---
  • Next, subtract 1 from P, which gives us 2.
  • 2^2<=7, so we put a 1 in the 2^2 column: 1101--
  • Subtract 4 from 7 to get 3. Subtract 1 from P to get 1.
  • 2^1<=3, so we put a 1 in the 2^1 column: 11011-
  • Subtract 2 from 3 to get 1. Subtract 1 from P to get 0.
  • 2^0<=1, so we put a 1 in the 2^0 column: 110111
  • Subtract 1 from 1 to get 0. Subtract 1 from P to get -1.
  • P is now less than zero, so we stop.

Another algorithm for converting decimal to binary

However, this is not the only approach possible. We can start at the right, rather than the left.
All binary numbers are in the form
a[n]*2^n + a[n-1]*2^(n-1)+...+a[1]*2^1 + a[0]*2^0
where each a[i] is either a 1 or a 0 (the only possible digits for the binary system). The only way a number can be odd is if it has a 1 in the 2^0 column, because all powers of two greater than 0 are even numbers (2, 4, 8, 16...). This gives us the rightmost digit as a starting point.

Now we need to do the remaining digits. One idea is to "shift" them. It is also easy to see that multiplying and dividing by 2 shifts everything by one column: two in binary is 10, or (1*2^1). Dividing (1*2^1) by 2 gives us (1*2^0), or just a 1 in binary. Similarly, multiplying by 2 shifts in the other direction: (1*2^1)*2=(1*2^2) or 10 in binary. Therefore
{a[n]*2^n + a[n-1]*2^(n-1) + ... + a[1]*2^1 + a[0]*2^0}/2
is equal to
a[n]*2^(n-1) + a[n-1]*2^(n-2) + ... + a[1]2^0 
Let's look at how this can help us convert from decimal to binary. Take the number 163. We know that since it is odd, there must be a 1 in the 2^0 column (a[0]=1). We also know that it equals 162+1. If we put the 1 in the 2^0 column, we have 162 left, and have to decide how to translate the remaining digits.
Two's column: Dividing 162 by 2 gives 81. The number 81 in binary would also have a 1 in the 2^0 column. Since we divided the number by two, we "took out" one power of two. Similarly, the statement a[n-1]*2^(n-1) + a[n-2]*2^(n-2) + ... + a[1]*2^0 has a power of two removed. Our "new" 2^0 column now contains a1. We learned earlier that there is a 1 in the 2^0 column if the number is odd. Since 81 is odd, a[1]=1. Practically, we can simply keep a "running total", which now stands at 11 (a[1]=1 and a[0]=1). Also note that a1 is essentially "remultiplied" by two just by putting it in front of a[0], so it is automatically fit into the correct column.
Four's column: Now we can subtract 1 from 81 to see what remainder we still must place (80). Dividing 80 by 2 gives 40. Therefore, there must be a 0 in the 4's column, (because what we are actually placing is a 2^0 column, and the number is not odd).
Eight's column: We can divide by two again to get 20. This is even, so we put a 0 in the 8's column. Our running total now stands at a[3]=0, a[2]=0, a[1]=1, and a[0]=1.
We can continue in this manner until there is no remainder to place.
  Let's formalize this algorithm:
1.  Let D= the number we wish to convert from decimal to binary.
2.  Repeat until D=0:
    a) If D is odd, put "1" in the leftmost open column, and subtract 1 from D.
    b) If D is even, put "0" in the leftmost open column.
    c) Divide D by 2.
    End Repeat
For the number 163, this works as follows:
1.  Let D=163
2.  b) D is odd, put a 1 in the 2^0 column.
  Subtract 1 from D to get 162.
    c) Divide D=162 by 2.
Temporary Result: 01     New D=81
D does not equal 0, so we repeat step 2.

2.  b) D is odd, put a 1 in the 2^1 column.
  Subtract 1 from D to get 80.
     c) Divide D=80 by 2.
Temporary Result: 11     New D=40
D does not equal 0, so we repeat step 2.

2.  b) D is even, put a 0 in the 2^2 column.
    c) Divide D by 2.
Temporary Result:011    New D=20

2.  b) D is even, put a 0 in the 2^3 column.
    c) Divide D by 2.
Temporary Result: 0011     New D=10

2.  b) D is even, put a 0 in the 2^4 column.
    c) Divide D by 2.
Temporary Result:  00011      New D=5

2.  a) D is odd, put a 1 in the 2^5 column.
       Subtract 1 from D to get 4.
    c) Divide D by 2.
Temporary Result:  100011   New D=2

2.  b) D is even, put a 0 in the 2^6 column.
    c) Divide D by 2.
Temporary Result:  0100011   New D=1

2.  a) D is odd, put a 1 in the 27 column.
       Subtract 1 from D to get D=0.
    c) Divide D by 2.
Temporary Result:  10100011   New D=0

D=0, so we are done, and the decimal number 163 is equivalent to the binary number 10100011.
Since we already knew how to convert from binary to decimal, we can easily verify our result. 10100011=(1*2^0)+(1*2^1)+(1*2^5)+(1*2^7)=1+2+32+128= 163.

ref: http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary

wanna practice what you learnt? try: TCHS SRM 27 250 pt: Brick Mystery: http://community.topcoder.com/stat?c=problem_statement&pm=7331&rd=10651

Sunday, June 29, 2014

TopCoder High School SRM 26 Level one - 250 pt - Third Buy Discount solution

TopCoder High School SRM 26 Level one - 250 pt - Third Buy Discount statement: http://community.topcoder.com/stat?c=problem_statement&pm=6745&rd=10650

TopCoder High School SRM 26 Level one - 250 pt - Third Buy Discount editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm26

TopCoder High School SRM 26 Level one - 250 pt - Third Buy Discount solution:

#include <iostream>
#include <vector>
#include <algorithm>

class ThirdBuyDiscount {
    public: double minCost(std::vector <int> p, int d) {
        int i=0;
        double cnt=.0;
        std::sort(p.begin(), p.end());
        while(i<p.size()-p.size()/3) cnt+=p[i], i++;
        while(i<p.size()) cnt+=(double)p[i]*(100-d)/100, i++;
        return cnt;
    }
};

TopCoder High School SRM 25 Level one - 250 pt - Many Numbers solution

TopCoder High School SRM 25 Level one - 250 pt - Many Numbers statement: http://community.topcoder.com/stat?c=problem_statement&pm=7276&rd=10077

TopCoder High School SRM 25 Level one - 250 pt - Many Numbers editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm25

TopCoder High School SRM 25 Level one - 250 pt - Many Numbers solution: 


#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

class ManyNumbers {
    public: int getSums(vector <int> n, int k) {
        int a=0, b=0;
        for(int i=0; i<n.size(); i++) {
            if(n[i]%k==0) a+=n[i];
            else b+=n[i];
        }
        return abs(a-b);
    }
};

TopCoder High School SRM 24 Level one - 250 pt - Answer Evaluation solution

TopCoder High School SRM 24 Level one - 250 pt - Answer Evaluation statement: http://community.topcoder.com/stat?c=problem_statement&pm=7272&rd=10076

TopCoder High School SRM 24 Level one - 250 pt - Answer Evaluation editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm24

TopCoder High School SRM 24 Level one - 250 pt - Answer Evaluation solution:

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

class AnswerEvaluation {
    public: int getScore(vector <string> k, vector <int> s, string a) {
        int cnt=0, c[30]={0};
        stringstream ss(a);
        string b;
        while(ss>>b) for(int i=0; i<k.size(); i++) if(b==k[i] && c[i]==0) { 
            cnt+=s[i];
            c[i]=1;
            break; 
        }
        return cnt;
    }
};

note: learn stringstream <sstream> in C++,

1 - http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/

2 - http://www.cplusplus.com/reference/sstream/stringstream/str/

but also peek other AC codes. i dont know stringstream of <sstream> either, but i think it keeps...
lemme dont explain my logic, cuz it is not proper i think.

TopCoder High School SRM 23 Level one - 250 pt - Good Exhibition solution

TopCoder High School SRM 23 Level one - 250 pt - Good Exhibition statement: http://community.topcoder.com/stat?c=problem_statement&pm=7278&rd=10075

TopCoder High School SRM 23 Level one - 250 pt - Good Exhibition editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm23

TopCoder High School SRM 23 Level one - 250 pt - Good Exhibition solution: 


#include <iostream>
#include <vector>
#include <map>
using namespace std;

class GoodExhibition {
    public: int numberOfPictures(vector <string> l, int k) {
        int cnt=0;
        map <string, int> a;
        for(int i=0; i<l.size(); i++) a[l[i]]++;
        for(map <string, int>::iterator i=a.begin(); i!=a.end(); i++) {
            if((*i).second>k-1) cnt+=k-1;
            else cnt+=(*i).second;
        }
        return cnt;
    }
};

Codeforces Round #161 (Div. 2), problem: (A) Beautiful Matrix solution

Codeforces Round #161 (Div. 2), problem: (A) Beautiful Matrix: http://codeforces.com/problemset/problem/263/A

Codeforces Round #161 (Div. 2), problem: (A) Beautiful Matrix editorial: http://codeforces.com/blog/entry/6419

Codeforces Round #161 (Div. 2), problem: (A) Beautiful Matrix solution: http://ideone.com/akttbJ


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    int a[6][6], ans, idx, idy;
    for(int i=0; i<5; i++) for(int j=0; j<5; j++) {scanf("%d", &a[i][j]); if(a[i][j]) idx=i, idy=j;}
    ans=abs(idx-2)+abs(idy-2);
    printf("%d", ans);
    return 0;
}

Codeforces Beta Round #91 (Div. 2 Only), problem: (A) Lucky Division solution & size/length of array C++

Codeforces Beta Round #91 (Div. 2 Only), problem: (A) Lucky Division: http://codeforces.com/problemset/problem/122/A

Codeforces Beta Round #91 (Div. 2 Only), problem: (A) Lucky Division editorial: http://codeforces.com/blog/entry/2956

Codeforces Beta Round #91 (Div. 2 Only), problem: (A) Lucky Division solution: http://ideone.com/I0guYW


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n, a[]={4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777}, flag=0;
    scanf("%d", &n);
    for(int i=0; i<sizeof(a)/sizeof(a[0]); i++) if(n%a[i]==0) {flag=1; break;}
    if(flag==1) printf("YES");
    else printf("NO");
    return 0;
}

size/length of array C++:

1 - http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array

2 - http://stackoverflow.com/questions/2037736/finding-size-of-int-array

3 - http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language/1975133#1975133

note: array-type is implicitly converted into a pointer when passed into function other than main().

Codeforces Round #163 (Div. 2), problem: (B) Queue at the School solution & swap c++

Codeforces Round #163 (Div. 2), problem: (B) Queue at the School solution & swap <algorithm> c++


Codeforces Round #163 (Div. 2), problem: (B) Queue at the School: http://codeforces.com/problemset/problem/266/B

Codeforces Round #163 (Div. 2), problem: (B) Queue at the School editorial: http://codeforces.com/blog/entry/6499

Codeforces Round #163 (Div. 2), problem: (B) Queue at the School solution: http://ideone.com/RWTq1a

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int main() {
    int n, t;
    char s[55];
    scanf("%d%d", &n, &t);
    scanf("%s", s);
    for(int i=0; i<t; i++) for(int j=1; j<n; j++) if(s[j-1]=='B' && s[j]=='G') swap(s[j-1], s[j]), j++;
    printf("%s", s);
    return 0;
}

swap <algorithm> c++: 

there are many swap functions in C++, but we need swap function of <algorithm>





Codeforces Round #103 (Div. 2), problem: (A) Arrival of the General solution

Codeforces Round #103 (Div. 2), problem: (A) Arrival of the General: http://codeforces.com/problemset/problem/144/A

Codeforces Round #103 (Div. 2), problem: (A) Arrival of the General editorial: http://codeforces.com/blog/entry/3693

Codeforces Round #103 (Div. 2), problem: (A) Arrival of the General solution: http://ideone.com/zVGB2I

#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;

int main() {
    int n, a[105], ma=INT_MIN, mi=INT_MAX, ima, imi, ans;
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d", &a[i]);
        if(a[i]>ma) ma=a[i], ima=i;
        if(a[i]<=mi) mi=a[i], imi=i;
    }
    ans=ima+(n-1-imi);
    if(imi<ima) ans--;
    printf("%d", ans);
    return 0;
}