Monday, April 10, 2017

Uri 2248 - Internship

#include <bits/stdc++.h>
using namespace std;

struct ans{
    int a = 0;
    int b = 0;
}rec[1511];

bool cmp(const ans &x, const ans &y){
    return x.b > y.b;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
     int n ,m,k,l,ca = 0;

     while(cin >> k and k){
          for(int i = 0; i < k ;i++){
            cin >> rec[i].a >> rec[i].b;
          }
          stable_sort(rec,rec+k,cmp);

     cout << "Turma " << ++ca << endl;

       bool b = true;

     for( int i = 1 ;i < k ; i++){
            if(b) cout << rec[i-1].a << " " ;

            if(rec[i].b != rec[i-1].b and b) {
                cout << endl ;
                    b = false;
             }
             rec[i-1].a = 0, rec[i-1].b = 0;
      }

      if(b) cout << rec[k-1].a << " " << endl;
      rec[k].a = 0, rec[k].b = 0;

      cout << endl ;
     }
    return 0;
}

Sunday, April 9, 2017

Uri 1652 - Deli Deli

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s,s1;
    int n,k;
    cin >> n >> k;
    map < string , string > ans;
    while(n--){
        cin >> s >> s1;
        ans.insert(pair < string , string > (s,s1));
            }
            while(k--){
                cin >> s;
                s1 = s;
                if(ans.count(s) != 0)
                    cout << ans.find(s)->second << endl;
                else{
                    if(s[s.size()-1] == 'y' and (s[s.size()-2] != 'a' and s[s.size()-2] != 'e' and s[s.size()-2] != 'i' and s[s.size()-2] != 'o' and  s[s.size()-2] != 'u'))
                        s[s.size()-1] = 'i',s +="es";
                    else if(s[s.size()-1] == 'o' or (s[s.size()-1] == 's') or (s[s.size()-2] == 'c' and (s[s.size()-1] == 'h')) or (s[s.size()-2] == 's' and s[s.size()-1] == 'h') or (s[s.size()-1] == 'x'))
                        s += "es";
                    else
                        s += "s";
                    cout << s << endl;
                    ans.insert(pair(s1,s));
                }
            }
    return 0;
}

Uri 1832 - EBCDIC

#include<bits/stdc++.h>
using namespace std;

int octal_to_decimal(int oc){
  int d = 0, i = 0, rem;
    while (oc != 0)
    {
        rem = oc % 10;
        oc /= 10;
        d += rem * pow(8, i);
        ++i;
    }
    return d;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    long n,k = 0;
    string s;
        int Tableebc2Asc[256] =
    {
            32,  32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 91, 46, 60, 40, 43, 33,
            38, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 93, 36, 42, 41, 59, 94,
            45, 47, 32, 32, 32, 32, 32, 32,
            32, 32,124, 44, 37, 95, 62, 63,
            32, 32, 32, 32, 32, 32,238,160,
            161, 96, 58, 35, 64, 39, 61, 34,
            230, 97, 98, 99,100,101,102,103,
            104,105,164,165,228,163,229,168,
            169,106,107,108,109,110,111,112,
            113,114,170,171,172,173,174,175,
            239,126,115,116,117,118,119,120,
            121,122,224,225,226,227,166,162,
            236,235,167,232,237,233,231,234,
            158,128,129,150,132,133,148,131,
            123, 65, 66, 67, 68, 69, 70, 71,
            72, 73,149,136,137,138,139,140,
            125, 74, 75, 76, 77, 78, 79, 80,
            81, 82,141,142,143,159,144,145,
            92, 32, 83, 84, 85, 86, 87, 88,
            89, 90,146,147,134,130,156,155,
            48, 49, 50, 51, 52, 53, 54, 55,
            56, 57,135,152,157,153,151, 32
    };
    while(getline(cin,s)){
            vector < string > ans;
            stringstream stl(s);
            string take;
            while (stl >> take)
            ans.push_back(take);

            for(int i = 0 ; i < ans.size() ; i++){
            stringstream str2(ans[i]);
            int p = 0;
            str2 >> p;
            unsigned char d = octal_to_decimal(p);
            (i != ans.size()-1)?printf("%c",Tableebc2Asc[d]) : printf("%c\n",Tableebc2Asc[d]);
            }
    }
    return 0;
}

Friday, April 7, 2017

Uri 2252 - Discovering Password

#include <bits/stdc++.h>
using namespace std;
typedef long double LD;

template
struct less_first {
    typedef pair < T1, T2 > type;
    bool operator ()(type const& a, type const& b) const {
        return a.first > b.first;
    }
};

int main()
{
    //freopen("in.txt","r",stdin);
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n,c = 0;
    while(cin >> n){
        map < LD, int > ans;
        LD a;
        for(int i = 0; i < 10 ;i++) {
                cin >> a;
         while(ans.count(a) != 0){
            a -= (LD)1e-15;
         }
        ans.insert(pair < LD,int > (a,i));
        }
     vector < pair < LD, int > > ans1(ans.begin(), ans.end());
     sort(ans1.begin(), ans1.end(), less_first < LD, int > ());
     vector > :: iterator it = ans1.begin();

     int i = 0;
     cout << "Caso " << ++c << ": ";
     for( vector < pair < LD, int > > :: iterator it = ans1.begin() ; it != ans1.end() ;it++,i++){
        if(i == n) break;
        cout << it->second ;
     }
        cout << endl;
    }
    return 0;
}

Uri 1945 - Simulator

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    string s;
    map < string , int > ans;
    int res;
    while(getline(cin,s)){

    stringstream str1(s);
    vector < string > variable;
    string take;
    while (str1 >> take)
    variable.push_back(take);

        int l = variable.size();

        if(l == 3){
        string ds = variable[2];

        if(isdigit(ds[0])){
            stringstream str2(variable[2]);
            int p = 0;
            str2 >> p;
            ans.insert(pair < string , int > (variable[0],p));
            res = p;
        }else{
            ans.insert(pair < string , int > (variable[0],ans.find(variable[2])->second));
            res = ans.find(variable[2])->second;
        }
    }else{
       ans.insert(pair < string , int > (variable[0],(ans.find(variable[2])->second) + ans.find(variable[4])->second));
       res = ans.find(variable[2])->second + ans.find(variable[4])->second ;
      }
    }
    cout << res << endl;
    return 0;
}

Thursday, April 6, 2017

Uri 2344 - Notas da Prova

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n;
    cin >> n;
    cout << ((n == 0)? 'E' : (n < 36)? 'D' : (n < 61)? 'C' : (n < 86)? 'B' : 'A') << endl;
    return 0;
}

Uri 1147 - Knight Scape

#include<bits/stdc++.h>
using namespace std;

int d_x[] = {1,1,2,2,-1,-1,-2,-2},
    d_y[] = {2,-2,1,-1,2,-2,1,-1};

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int idx = 0,n;
    char ch;
    while(cin >> n >> ch and n ){
        int res = 0;
        vector < vector < int> > ans(8, vector(8));
        int x = n - 1,
            y = ch - 'a';
        for(int i = 0; i < 8 ; i++){
            cin >>  n >> ch;
            ans[n - 1][ch - 'a'] = 1;
        }

        for(int i = 0; i < 8 ; i++){
                int x1 = x + d_x[i],
                    y1 = y + d_y[i];
        if(x1 >= 0 and x1 < 8 and y1 >= 0 and y1 < 8){
            int x2 = x1 + 1,
                y2 = y1 - 1,
                k = 1;

            if(x2 >= 0 and x2 < 8 and y2 >= 0 and y2 < 8 and ans[x2][y2]) k = 0;
            y2 = y1 + 1;
            if(x2 >= 0 and x2 < 8 and y2 >= 0 and y2 < 8 and ans[x2][y2]) k = 0;
            if(k) res++;
        }
    }
        cout << "Caso de Teste #" << ++idx << ": " << res << " movimento(s)." << endl;
    }
    return 0;
}

Uri 1403 - Grandpa is Famous


#include<bits/stdc++.h>
using namespace std;

template
struct less_second {
    typedef pair < T1, T2 > type;
    bool operator ()(type const& a, type const& b) const {
        if(a.second != b.second)
        return a.second > b.second;

        return a.first < b.first;
    }
};

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n,m,p = 0;
    int t;
    while(cin >> n >> m and (n+m) != 0){
    int k = 0 , p = 0;
    map < int , int > ans;
    for(int i = 0; i < n*m ; i++){
          cin >> t;
          ans[t]++;
     }

     vector < pair < int, int > > ans1(ans.begin(), ans.end());
     sort(ans1.begin(), ans1.end(), less_second < int, int >());
     vector < pair < int, int > > :: iterator it = ans1.begin();
     it++;

     for(;it != ans1.end() ; it++,p = 1){
             if(k == 0) k = it->second ;
             if( k == (it->second)){
             cout << it->first << " " ;
             }else{
                break;
             }
        }
         cout << endl;
    }
    return 0;
}

Uri 1936 - Factorial

#include<bits/stdc++.h>
using namespace std;

int main()
{
     ios_base::sync_with_stdio(false);cin.tie(NULL);
    int ar[10] = {0,1,2,6,24,120,720,5040,40320,362880};
    int n,c = 0,i,n1 = 0,l1;
    cin >> n;
    l1 = n;
    while(l1!=0){
        for(i = 1;  ; i++) if(ar[i] >= l1 or i > 8) break;
         (ar[i] <= l1)? l1 -= ar[i] : l1 -= ar[i-1];
          c++;
    }
    cout << c << endl;
    return 0;
}

Wednesday, April 5, 2017

Uri 1800 - Where Are My Keys

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);

    int week_office,two_days_office,k;
    cin >> week_office >> two_days_office;

    set < int > two_days_office_s;

    for(int i = 0; i < two_days_office ;i++){
         cin >> k;
         two_days_office_s.insert(k);
    }

    while(week_office--){
        cin >> k;
        cout << ((two_days_office_s.count(k))? 0 : 1 ) << endl;
        two_days_office_s.insert(k);
    }

    return 0;
}



Tuesday, April 4, 2017

Uri 1895 - Game of Limit

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n,c,t_i,l,Ali=0,Bo=0,c_b;
    cin >> c >> t_i >> l;
    for(int i = 0; i < c-1 ; i++){
        cin >> c_b;
        n = abs(c_b - t_i);
        if(n <= l){
            (i&1)? Bo += n : Ali += n;
            t_i = c_b;
        }
    }
    cout << Ali << " " << Bo << endl;
    return 0;
}

Uri 2134 - Who Will Fail?

#include<bits/stdc++.h>
using namespace std;

struct mode{
    string name;
    int problem_num;
}recod[1001];

bool cmp(const mode &a, const mode &b){
    if(a.problem_num != b.problem_num)
        return a.problem_num < b.problem_num ;
    return a.name > b.name;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n,case_n = 0,t = 0;
    while(cin >> n){
            if(t != 0) cout << endl;
    for(int i = 0; i < n ;i++){
        cin >> recod[i].name >> recod[i].problem_num;
    }
    stable_sort(recod,recod+n,cmp);
    cout << "Instancia " << ++case_n << endl << recod[0].name << endl;
    for(int i = 0; i < n ;i++)
       recod[i].name.clear(),recod[i].problem_num = 0;
    t=1;
    }
    cout << endl;
    return 0;
}

Uri 1845 - Efilogue


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    char ch;
    bool check;
    set < char > sd,sd1;
    sd.insert('J');sd1.insert('j');sd.insert('B');sd1.insert('b');
    sd.insert('V');sd1.insert('v');sd.insert('Z');sd1.insert('z');
    sd.insert('S');sd1.insert('s');sd.insert('P');sd1.insert('p');
    sd.insert('X');sd1.insert('x');
    while((ch = getchar()) != EOF)
    {
        if(sd.count(ch) != 0) ch = 'F';
        else if(sd1.count(ch) != 0)ch = 'f';

        if(!((ch == 'f' or ch == 'F') and check))cout << ch;
        if(ch == 'f' or ch == 'F')check = true;
        else check = false;
    }
    return 0;
}

Uri 1898 - Kickback Sum


#include<bits/stdc++.h>
using namespace std;

int c = 0,cr = 0;

string add_big_double_value(string fr,string se,int carry){

    string res="",check1 = "",check2 = "";

    int fr_l = fr.size(),
        se_l = se.size();

    if(fr_l < se_l) swap(fr,se);
            int len = fr.size(),
                se_len = se.size();

    if(cr)reverse(se.begin(),se.end());
    for(int i = se_len ; i < len ; i++) se += '0';
    if(cr)reverse(se.begin(),se.end());
    int digit = 0; c = carry;

    for(int i = len-1 ; i >= 0 ; i--){
            digit = (fr[i] - '0') + (se[i] - '0') + c;
            res += (digit > 9)? (digit - 10) + '0' : digit + '0' ;
            (digit > 9)? c = 1 : c = 0;
    }
    cr = 1;
    reverse(res.begin(),res.end());
    return res;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    long long cpf_n;
    long poi;
    int p1 = 0,p2 = 0,p = 0;
    bool b = false;
    string s,s1,input1="",input2="",cpf,se_res,point_str1 = "" ,point_str2 = "",dec_str;
    cin >> s >> s1;
    for(int i = 0; i < s.size() ;i++){
        if(((s[i] >= '0' and s[i] <= '9') or s[i] == '.') and cpf.size() < 11){
            if(b and s[i] == '.') {
                    cpf += s[i];
                    break;
            }
            cpf += s[i];
            if(b) p++;
            if(p>=2) {
                if(cpf.size() < 11) return cout << "cpf " << cpf << endl , 0;
                break;
            }
            if(s[i] == '.'){
                b = true;
            }
        }else if(((s[i] >= '0' and s[i] <= '9') or s[i] == '.') and cpf.size() >= 11){
                    if(b and s[i] == '.') break;
                    if(p >= 2)break;
                    if(b) p++;
                    (b)?(input1.size() < 2)? input1 += s[i] : point_str1 += s[i] : (s[i] != '.')? input1 += s[i] : input1 = input1;
                    if(s[i] == '.') b = true;
                }
            }

     b = false;
     p = 0;

    for(int i = 0; i < s1.size() ;i++){
       if((s1[i] >= '0' and s1[i] <= '9') or s1[i] == '.'){
                     if(b and s1[i] == '.') break;

                    (b)? point_str2 += s1[i] : (s1[i] != '.')? input2 += s1[i] : input2 = input2;

                    if(b) p++;
                    if(p >= 2)break;
                    if(s1[i] == '.') b = true;
                    }
    }
    if(point_str1.empty()) point_str1 = "00";if(point_str2.empty()) point_str2 = "00";
    se_res = add_big_double_value(point_str1,point_str2,c);
    dec_str = add_big_double_value(input1,input2,c);
    if(c) dec_str += '1';

    cout << "cpf " << cpf << endl << dec_str  <<   "."  << se_res << endl;
    return 0;
}

Monday, April 3, 2017

Uri 1868 - Square Spiral

#include<bits/stdc++.h>
using namespace std;

int x_dr[] = {0,-1,0,1};
int y_dr[] = {1,0,-1,0};
int x,y;

void show_result(vector < vector < char > > &ch){
  for (int i = 0; i < ch.size(); i++) {
    for (int j = 0; j < ch.size(); j++) {
      cout << ch[i][j];
    }
    cout << endl;
  }
  cout << "@" << endl;
}

bool moving(vector < vector < char > > &ch,int item,int d){
    int n = ch.size();
    for(int i = 0; i < item ;i++){
        int new_x = x + x_dr[d],new_y = y + y_dr[d];

        if(new_x < 0 or new_x >= n or new_y < 0 or new_y >= n) return false;

        ch[x][y] = 'O';
        x = new_x ,y = new_y;
        ch[x][y] = 'X';
        show_result(ch);
    }
 return true;
}

int main()
{
    int n;
    while(cin >> n and n != 0){
        vector < vector < char > > ch(n,vector < char > (n, 'O'));
        int item = 0;
        x = n >> 1;
        y = n >> 1;
        ch[x][y] = 'X';
        show_result(ch);
        while(true){
            item++;
        if(!moving(ch,item,0)) break;
        if(!moving(ch,item,1)) break;
        item++;
        if(!moving(ch,item,2)) break;
        if(!moving(ch,item,3)) break;
        }
    }
    return 0;
}

Sunday, April 2, 2017

Uri 1254 - Tag Replacement

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s,s1;
    while(cin >> s >> s1){
            cin.ignore();
        set < char > let;

        bool b = false;
        string input,word,word1;

        getline(cin,input);
        transform(s.begin(),s.end(),s.begin(), ::tolower);

        for(int i = 0; i < s.size() ;i++) let.insert(s[i]);

        for(int i = 0; i < input.size() ;i++){
            if(isalpha(input[i]) or (input[i] >= '0' and input[i] <= '9')){

             word += input[i];
             word1 = word;
             transform(word1.begin(),word1.end(),word1.begin(), ::tolower);

                if(word1 == s and b){
                cout << s1;
                word.clear();
                word1.clear();
                 }

                if(word.size() == 1){
                    if(let.count(tolower(input[i])) == 0) {
                        cout << input[i];
                        word1.clear();
                        word.clear();
                    }
                 }
            }else if(input[i] == ' ' and word1 == s and b){
                     cout << s1 << " ";
                     word.clear();
            }else if(input[i] == '>' and word1 == s and b){
                     cout << s1 << ">";
                     word.clear();
                     b = false;
            }else if(input[i] == '<'){
                     b = true;
                     (word.empty())? cout << '<' : cout << word << "<";
                     word.clear();
            }else if(input[i] == '>'){
                     (word.empty())?cout << '>' : cout << word << ">";
                     word.clear();
                     b = false;
                }else {
                    (word.empty())? cout << input[i] : cout << word << input[i];
                     word.clear();
            }
        }
        if(!word.empty()) cout << word << endl;
        else cout << endl;
    }
    return 0;
}

Uri 2523 - Will's Message

#include<bits/stdc++.h>
using namespace std;

int main()
{
    char s[1001];
    int n,l;
    while(scanf("%s",s) != EOF){
        scanf("%d",&n);
        while(n--){
                scanf("%d",&l);
            printf("%c",s[l-1]);
        }
        printf("\n");
    }
    return 0;
}

Uri 2502 - Deciphering the Encrypted Card

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m;
    string orginal_input,result;
    string s1,s2;
    while(cin >> n >> m){
          multimap < char , int > st1,st2;
          cin.ignore();
          getline(cin,s1);
          getline(cin,s2);
          for(int j = 0 ; j < n ;j++){
            st1.insert(pair < char , int >(s1[j],j));
            st2.insert(pair < char , int >(s2[j],j));
          }
        int index ;
        while(m--){
            getline(cin,orginal_input);
            for(int i = 0;  i < orginal_input.size() ; i++){
                    char ch = toupper(orginal_input[i]);
                if(st1.count(ch) == 0 && st2.count(ch) == 0){
                        result += orginal_input[i];
                }else if(st1.count(ch) != 0){
                    index = st1.find(ch)->second;
                    result += !isupper(orginal_input[i]) ? (char) tolower(s2[index]) : s2[index];
                }else if(st2.count(ch) != 0){
                    index = st2.find(ch)->second;
                    result += !isupper(orginal_input[i]) ? (char) tolower(s1[index]) : s1[index];
                }
            }
            cout << result << endl;
            result.clear();
        }
        cout << endl;
    }

    return 0;
}

Saturday, April 1, 2017

Uri 2520 - The Last Analógimôn

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,a,b,c,r;
    while(scanf("%d %d",&n,&m) != EOF){
       int ans[101][101];
     for(int i = 0;  i < n ;i++){
       for(int j = 0; j < m ;j++){
        scanf("%d",&ans[i][j]);
        if(ans[i][j] == 1) a = i,b = j;
        if(ans[i][j] == 2) r = i, c = j;
       }
     }
       int res = 0;
       res = abs(c - b) + abs(r - a);
       printf("%d\n",res);
    }
    return 0;
}

Uri 1276 - Letter Range

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int ans[27];
    string s;
    while(getline(cin,s)){
        memset(ans,0,sizeof(ans));
        set < char > res;
        int p = 0;
        for(int i = 0; i < s.size() ;i++){
            ans[s[i] - 'a'] = 1;
            res.insert(s[i]);
        }
        char ch = '#';
        for(int i = 0; i < 26 ;i++){
            if(ans[i]){
                if(ch == '#') ch = (char)(i+'a');
            }else{
                if(res.count(ch) != 0){
                    if((ch - 'a') - i == 1)
                         (p)? cout << ", " << ch << ":" << ch : cout << ch << ":" << ch;
                    else
                         (p)? cout << ", " << ch << ":" << (char) ((i-1)+'a') : cout << ch << ":" << (char)((i-1)+'a') ;
                p=1;
                }
                ch = '#';
            }
        }
        if(ch != '#')(p)?cout << ", " << ch << ":" << 'z' :cout << ch << ":" << 'z' ;
        cout << endl;
    }
    return 0;
}