Friday, February 28, 2014

Codeforces Round #232 (Div. 2), problem: (A) On Segment's Own Points solution

Codeforces Round #232 (Div. 2), problem: (A) On Segment's Own Points: http://codeforces.ru/contest/397/problem/A

Codeforces Round #232 (Div. 2), problem: (A) On Segment's Own Points solution: http://ideone.com/WJ0C5y
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int n, l, r, a, b, arr[110], cnt, flag;
    scanf("%d", &n);
    scanf("%d%d", &a, &b);
    cnt=flag=0;
    memset(arr, 0, sizeof(arr));
    for(int i=a; i<b; i++) arr[i]++;
    for(int i=1; i<n; i++) {
        scanf("%d%d", &l, &r);
        for(int j=l; j<r; j++) arr[j]++;
    }
    for(int i=a; i<b; i++) {
        if(arr[i]==1) cnt++;
    }
    printf("%d\n", cnt);
    return 0;
}

Tuesday, February 25, 2014

Codeforces Round #231 (Div. 2) problem A - Counting Sticks solution

Codeforces Round #231 (Div. 2) problem A - Counting Sticks : http://codeforces.ru/contest/394/problem/A

Codeforces Round #231 (Div. 2) problem A - Counting Sticks solution : http://ideone.com/mx3VZO
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int a, b, c;
    a=b=c=0;
    char s[1000];
    scanf("%s", s);
    int i=0;
    for(i=0; s[i]!='+'; i++) a++;
    for(i++; s[i]!='='; i++) b++;
    for(i++; i<strlen(s); i++) c++;
    if(a+b==c) printf("%s", s);
    else if(c>1 && a+b==c-2) {
        for(i=0; i<a+1; i++) printf("|");
        printf("+");
        for(i=0; i<b; i++) printf("|");
        printf("=");
        for(i=0; i<c-1; i++) printf("|");
    }
    else if((a>1 || b>1) && a+b==c+2) {
        if(a>1) {
            for(i=0; i<a-1; i++) printf("|");
            printf("+");
            for(i=0; i<b; i++) printf("|");
            printf("=");
            for(i=0; i<c+1; i++) printf("|");
        }
        else {
            for(i=0; i<a; i++) printf("|");
            printf("+");
            for(i=0; i<b-1; i++) printf("|");
            printf("=");
            for(i=0; i<c+1; i++) printf("|");
        }
    }
    else printf("Impossible");
    return 0;
}

Monday, February 24, 2014

codeforces easy round problem D2 - Digit's Sum (subtask 2) solution

codeforces easy round problem D2 - Digit's Sum (subtask 2) : http://codeforces.com/gym/200470/problem/D2
D2. Digit's sum(subtask 2)
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
Andrew was sitting and counting the sum of digits of the number, but while doing that he forgot what the source number was. Help him to restore the original number, if he knows that the sum is n. If there are several variants, find the smallest. Subtask 1 (1 <  = n <  = 50)Subtask 2 (1 <  = n <  = 50000)
Input
The only line of input file contains the number n (1 <  = n <  = 50000)
Output
In a single line of the output file output a single number - the answer to the problem.
Sample test(s)
input
3
output
3
input
10
output
19

codeforces easy round problem D2 - Digit's Sum (subtask 2) solution :
http://codeforces.com/gym/200470/submission/6100648
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int a;
    long long b[1039999];
    scanf("%d", &a);
    int i=0;
    while(a) {
        if(a<10) {
            b[i]=a;
            a=a-a;
        }
        else {
            b[i]=9;
            a=a-9;
        }
        i++;
    }
    for(int j=i-1; j>=0; j--) printf("%d", b[j]);
    return 0;
}

codeforces easy round problem D1 - Digit sum (subtask1) solution

codeforces easy round problem D1 - Digit sum (subtask1) : http://codeforces.com/gym/200470/problem/D1

D1. Digit summ(subtask 1)
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
Andrew was sitting and counting the sum of digits of the number, but while doing that he forgot what the source number was. Help him to restore the original number, if he knows that the sum is n. If there are several variants, find the smallest. Subtask 1 (1 <  = n <  = 50)Subtask 2 (1 <  = n <  = 50000)
Input
The only line of input file contains the number n (1 <  = n <  = 50)
Output
In a single line of the output file output a single number - the answer to the problem.
Sample test(s)
input
6
output
6
input
10
output
19

codeforces easy round problem D1 - Digit sum (subtask1) solution :
http://codeforces.com/gym/200470/submission/6100619
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int a, b[1000];
    scanf("%d", &a);
    int i=0;
    while(a) {
        if(a<10) {
            b[i]=a;
            a=a-a;
        }
        else {
            b[i]=9;
            a=a-9;
        }
        i++;
    }
    for(int j=i-1; j>=0; j--) printf("%d", b[j]);
    return 0;
}

codeforces easy round problem C1 - Nice Array Solution

codeforces easy round problem C1 - Nice Array : http://codeforces.com/gym/200470/problem/C1
C1. Nice array
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
Let’s call an array ‘nice’ if the result of some permutation of it, you can get a series of numbers that form an arithmetic progression. I want to know whether the specified array is nice.
Input
The first line of the input file contains a natural n (2 <  = n <  = 100) The second string contains n integers ( - 1000 <  = ai <  = 1000)
Output
In a single line of output file output "YES" or "NO" (without the quotes) - depending on whether the array of nice
Sample test(s)
input
3
2 3 1
output
YES
input
3
2 3 2
output
NO
Note
In the first test array can be written as a sequence of 1 2 3 - arithmetic progression with difference 1, the second is impossible.

codeforces easy round problem C1 - Nice Array Solution :  http://codeforces.com/gym/200470/submission/6100552
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int cmp(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);}

int main() {
    int a, b[2005], cnt, c;
    scanf("%d", &a);
    cnt=0; 
    for(int i=0; i<a; i++) scanf("%d", &b[i]);
    qsort(b, a, sizeof(int), cmp);
    c=b[1]-b[0];
    for(int i=1; i<a; i++) {
        if(b[i]-b[i-1]==c) cnt++;
    }
    if(cnt+1==a) printf("YES");
    else printf("NO");
    return 0;
}

codeforces easy round problem B1 - the smallest number solution & stdin from .txt text files & stdout to .txt text files

codeforces easy round problem B1 - the smallest number :
http://codeforces.com/gym/200470/problem/B1

B1. The smallest number
time limit per test
1 second
memory limit per test
64 megabytes
input
input.txt
output
output.txt
At school, Jonny learns to compare numbers. But he wants to play again, and again you have to do it for him. You need to find the smaller of the two numbers. Note that for input-output files are used!
Input
The only line of input file contains two integers a and b ( - 106 <  = a, b <  = 106) It is guaranteed that a is not equal to b
Output
In a single line of output file output-singular answer to the problem
Sample test(s)
input
2 -3
output
-3
input
1 2
output
1
codeforces easy round problem B1 - the smallest number solution : http://codeforces.com/gym/200470/submission/6100763
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    //#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    //#endif
    int a, b;
    scanf("%d%d", &a, &b);
    if(a<b) printf("%d", a);
    else printf("%d", b);
    return 0;
}

note: for input .txt - text files and output .txt - text files from c++(cpp) language just use :
 freopen("input.txt", "rt", stdin);
 freopen("output.txt", "wt", stdout);

codeforces easy round problem A1 - A+B solution

codeforces easy round problem A1 - A+B : http://codeforces.com/gym/200470/problem/A1
A1. A+B
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Jonny’s task at school was many problems of finding a wallet of two numbers. The boy wants to play, so he's too lazy to do it. Write a program that will do everything for him.
Input
The only line of input file contains two integers a and b ( - 106 <  = a, b <  = 106)
Output
In a single line of output file output-singular answer to the problem
Sample test(s)
input
2 2
output
4
input
-2 2
output
0

codeforces easy round problem A1 - A+B solution : http://codeforces.com/gym/200470/submission/6100465
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    long long a, b;
    scanf("%I64d%I64d", &a, &b);
    printf("%I64d", a+b);
    return 0;
}

codechef FEB14 LCPESY - Longest Common Pattern solution

codechef February 2014 Challenge FEB14 LCPESY - Longest Common Pattern: http://www.codechef.com/problems/LCPESY

codechef FEB14 LCPESY - Longest Common Pattern solution: http://www.codechef.com/viewsolution/3383472
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. int main() {
  7. int t, cnt, a[100], b[100];
  8. char s1[10005], s2[10005];
  9. scanf("%d", &t);
  10. while(t--) {
  11. cnt=0;
  12. for(int i=0; i<100; i++) a[i]=b[i]=0;
  13. scanf("%s\n%s", s1, s2);
  14. for(int i=0; i<strlen(s1); i++) a[s1[i]-48]++;
  15. for(int i=0; i<strlen(s2); i++) b[s2[i]-48]++;
  16. for(int i=0; i<100; i++) {
  17. if(a[i]<b[i]) cnt += a[i];
  18. else cnt += b[i];
  19. }
  20. printf("%d\n", cnt);
  21. }
  22. return 0;
  23. }

note: memset() gave TLE


Monday, February 17, 2014

Rockethon 2014 problem B - Word Folding solution

Rockethon 2014 problem B - Word Folding :  http://codeforces.com/contest/391/problem/B

Rockethon 2014 problem B - Word Folding solution :  http://ideone.com/43qAoS

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

int main() {
    int a[1005], max;
    char s[1005];
    scanf("%s\n", s);
    memset(a, 0, sizeof(a));
    max=0;
    for(int i=0; i<strlen(s); i++) {
        for(int j=i-1; j>=0; j-=2) {
            if(s[i]==s[j] && a[j]+1>a[i]) a[i]=a[j]+1;
        }
        if(a[i]>max) max=a[i];
    }
    printf("%d\n", max+1);
    return 0;
}

note: during contest i couldnt solve it, so after contest i glanced AC codes of tourist and rng_58, i liked both. well, use dynamic programming, i mean calculate only once. loop i from 0 to strlen(s), and j from i-1 to 0. however, decrement j by 2(j-=2), because when folding the string, symmetrical representation of i goes back by 2. and update maximum resemblance (if(a[i]>max) max=a[i];)