2012年7月16日月曜日

AOJ Volume11 1179: Millennium



/*******************************
* 2012/07/06                   *
* AOJ Volume11 1179            *
* Millennium                   *
* crane                        *
********************************/
#include <iostream>
using namespace std;


#define MAX_N 1001
int field[MAX_N][11];

int main() {


    for(int i=1; i<=999; i++){
        for(int j=1; j<=10; j++){
            field[i][j] = 0;
        }
    }
    int  frag = 0;
    for(int i=1; i<=999; i++){
        frag = 0;
        if(i % 3 == 0)
            frag = 1;
        for(int j=1; j<=10; j++){
            if(frag == 1)
                field[i][j] = 20;
            else{
                if(j%2 == 0){
                    field[i][j] = 19;
                }else{
                    field[i][j] = 20;
                }

            }
        }
    }


    /* input  */
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        int y=0, m=0, d=0;
        cin >> y >> m >> d;
        long long  sum=0;
        int frag2=0;
        int a = m;
        for(int j=y; j<1000; j++){
            if(frag2 == 1)  a=1;

            for(int k=a; k<=10; k++){
                if(frag2 == 0){
                    sum += field[j][k] - d;
                    frag2 = 1;
                }else{
                    sum += field[j][k];
                }
            }
        }
        cout << sum+1 << endl;
    }

    return 0;
}

2012年7月15日日曜日

AOJ Volume1 0158: Collatz's Problem

  • n が偶数の時は 2 で割る。
  • n が奇数の時は 3 倍し、1 を足す。
  • 整数 n は1以上でかつ上記の計算を繰り返す途中の値が1000000以下となる程度の整数とあったのでint からlongに変更。

/******************************
* 2012/07/15                  *
* AOJ_Volume1_0158            *
* Collatz's Problem           *
* crane                       *
*******************************/
#include<iostream>
using namespace std;
int main(){
    long long int n;
    while(cin >> n, n){
        int count = 0;
        while(1){
            if(n == 1) break;
            if(n % 2 == 0) n /= 2;
            else          n = (n*3) + 1;
            count++;
        }
        cout << count << endl;
    }
    return 0;
}

AOJ Volume5 0538: IOIOI

単純に回したのでは時間切れとなったので
考え方を変更した。

P1ならIOI  P2ならIOIOI
となるはずなので、IOIの連続出現回数をカウントしていく。
P1なら1回、P2なら2回連続で出現したらresをインクリメント
/*****************************
* 2012/07/15                 *
* AOJ_Volume5_0538           *
* IOIOI                      *
* crane                      *
******************************/
#include<iostream>
#include<string>
using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        int m;
        cin >> m;
       
        string s;
        cin >> s;
        int count=0, res= 0;
            for(int i=0; i<m;){
                if(s.substr(i, 3) == "IOI"){
                    count++;
                    if(n <= count) res++;
                }else {
                    count = 0;
                }
               
                if(count == 0) i++;
                else              i+=2;   
            }
        cout << res << endl;
    }
    return 0;
}

2012年7月14日土曜日

AOJ Volume11 1129: Hanafuda Shuffle (POJ1978)

問題文通りにそのまま書いてみた。


/********************************
*    2012/0623                    *
*   AOJ VOlume11 1129           *
*    POJ1978                     *
*   花札シャッフル              *
*    crane                       *
********************************/

#include<iostream>

#define MAX_N 50
int card[MAX_N];    //カード内容
int t_card[MAX_N];    //シャッフル用カードtmp

int main(){

    int n = 0;    //札の枚数
    int r = 0;    //カット回数
    int p = 0;    //p枚目から
    int c = 0;    //c枚取り出す

    while(std::cin >> n >> r, n, r){
        //配列の初期化
        for(int i=0; i<n; i++)        card[i] = i + 1;
       
        //shuffle
        for(int i=0; i<r; i++){    //カット回数r分
            std::cin >> p >> c;

            for(int j=0; j<c; j++)
                t_card[c-1-j] = card[n-p-j];

            for(int j=n-p+1; j<n; j++)
                card[j-c] = card[j];

            for(int j=0; j<c; j++)
                card[n-c+j] = t_card[j];
        }

        std::cout << card[n-1] << std::endl;

    }
    return 0;
}

AOJ Volume20 2013: Osaki

hh:mm:ss で与えられるため秒に統一する。
hh  ->*60*60
mm->*60
ss  
その時間に出発した電車、到着した電車の数をカウントする。
その時間にどれだけの電車が走っているかをカウントし、その最大値が答えになる。



 /*********************************
* 2012/0714                      *
* AOJ_Volume20_2013              *
* Osaki                          *
* crane                          *
**********************************/
#include<iostream>
#include<cstdio>
using namespace std;

#define MAX_N 24*60*60
int field[MAX_N];


int main(){
    int n;
    while(cin >> n, n){
        for(int i=0; i<MAX_N; i++)
            field[i] = 0;

        int h1,m1,s1,h2,m2,s2;
        for(int i=0; i<n; i++){
            scanf("%d:%d:%d %d:%d:%d", &h1, &m1, &s1, &h2, &m2, &s2);
            int t1 = (h1 * 3600) + (m1 * 60) + s1;//秒に統一
            int t2 = (h2 * 3600) + (m2 * 60) + s2;
            field[t1]++;    //その時間に出発した電車をカウント
            field[t2]--;    //その時間に到着した電車をカウント
        }
       
        int m_count=0, count=0;
        for(int i=0; i<MAX_N; i++){//その時間に走っている車両数の最大値が答えになる。
            count += field[i];
            m_count = max(m_count, count);
        }
        cout << m_count << endl;       
    }
    return 0;
}


AOJ Volume2 0206: Next Trip

/****************************
* 2012/07/14                *
* AOJ_Volume2_0206          *
* Next Trip                 *
* crane                     *
*****************************/

#include<iostream>
using namespace std;

int main(){

    int pri;
    while(cin >> pri, pri){
        int res;
        bool frag = false;
        for(int i=1; i<=12; i++){
            int m,n;
            cin >> m >> n;
            pri -= m-n;
            if(pri <= 0 && frag != true){
                frag = true;
                res = i;
            }
        }
        if(frag) cout << res << endl;
        else     cout << "NA" << endl;
    }
    return 0;
}

AOJ Volume1 0149: Eye Test

判定関数においてA~Dそれぞれの範囲をわかりやすくするため
すべての下限上限を書いた。
A~Dの配列0~4だけどmap<char, int>なんかで扱っても良かったかも。

/****************************
* 2012/07/14                *
* AOJ_Volume1_0149          *
* Eye Test                  *
* crane                     *
*****************************/
//--------------------------------------//
//        判定    視力                    //
//    A    1.1以上                            //
//    B    0.6以上1.1未満                    //
//    C    0.2以上0.6未満                    //
//    D    0.2未満                            //
//--------------------------------------//

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

int solve(double x);

int main(){
    int ldata[4] = {0, 0, 0, 0};
    int rdata[4] = {0, 0, 0, 0};
   
    double left, right;
    while(cin >> left >> right){
        int le = solve(left);
        int ri = solve(right);

        ldata[le]++;
        rdata[ri]++;
    }

    for(int i=0; i<4; i++)
        cout << ldata[i] << " " << rdata[i] << endl;
   return 0;
}

int solve(double x){
    if(1.1 <= x)                       return 0;
    else if(0.6<=x && x<1.1)  return 1;
    else if(0.2<=x && x<0.6)  return 2;
    else if(x<0.2)                     return 3;
}

2012年7月13日金曜日

AOJ Volume1 0134: Exit Survey

int sumでは足りないのでlong longで対応
/*****************************
* 2012/07/13                 *
* AOJ_Volume1_0134           *
* Exit_Survey                *
* crane                      *
******************************/
#include<iostream>
using namespace std;

int main(){
    int n, input;
    cin >> n;
    long long int sum = 0;
    for(int i=0; i<n; i++){
        cin >> input;
        sum += input;
    }
    cout << sum/n << endl;
    return 0;
}

AOJ Volume1 0127: Pocket Pager Input

/*********************************
* 2012/07/13                     *
* AOJ_Volume1_0127               *
* Pocket_Pager_Input             *
* crane                          *
**********************************/

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

char data[7][6] ={
    {},
    {' ','a','b','c','d','e'},
    {' ','f','g','h','i','j'},
    {' ','k','l','m','n','o'},
    {' ','p','q','r','s','t'},
    {' ','u','v','w','x','y'},
    {' ','z','.','?','!',' '}
};

int main(){

    string input;
    while(1){
        getline(cin, input);
        if(input.empty())break;

        bool frag = true;
        string output = "";
   
        if(input.length() % 2 != 0)
            frag = false;

        for(int i=0; i<input.length() && frag==true; i+=2){
                int in1 = (int) input.at(i) - '0';
                int in2 = (int) input.at(i+1) - '0';
                if(0<in1 && in1 < 7 && 0 < in2 && in2 < 6)
                    output += data[in1][in2];
                else
                    frag = false;
        }
        if(frag) cout << output << endl;
        else     cout << "NA" << endl;
    }
    return 0;
}

2012年7月11日水曜日

AOJ Volume0 013: Switching Railroad Cars

stackを用いる。

/*****************************
* 2012/07/11                 *
* AOJ_Volume0_0013           *
* Switching_Railroad_Cars    *
* crane                      *
******************************/


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

int main(){
    stack<int> data;
    int n;
    while(cin >> n){
        if(n == 0){
            cout << data.top() << endl;
            data.pop();
        }else data.push(n);
    }
    return 0;
}