0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

競プロ日記#25/04/12

Posted at

アルゴ式-キューを配列で実装

int main() {
   int N,Q; 
   cin >> N >> Q;
   vector<int> A(N,-1);
   int head,tail;
   head = 0;
   tail = 0;

    for (int i = 0;i < Q;i++){
        int n;
        cin >> n;
        if(n == 0){
            int v;
            cin >> v;
            A[tail] = v;
            tail++;
            if(tail == N){
                tail = 0;
            } 
        }
        if(n == 1){
            A[head] = -1;
            head++;
            if(head == N){
                head = 0;
            }
        }
    }
    for (int i = 0;i < N;i++){
        cout << A[i] << endl;
    }

}

アルゴ式-タスクリスト

int main() {
   int Q; 
   cin >> Q;
   vector<string> A(100000,"");
   int head,tail;
   head = 0;
   tail = 0;

    for (int i = 0;i < Q;i++){
        int n;
        cin >> n;
        if(n == 0){
            string v;
            cin >> v;
            A[tail] = v;
            tail++;
            if(tail == 100000){
                tail = 0;
            } 
        }
        if(n == 1){
            cout << A[head] << endl;
            A[head] = "";
            head++;
            if(head == 100000){
                head = 0;
            }
        }
    }
}

アルゴ式-貯金計画

  • 公差を求めれば答えは簡単に求められるがこれで良いのか感あった(解説も公差dからX - A*Xしてるのであってた)
  • 念のためFocusGold2Bを引っ張ってもっといい求め方ないか考えたが微妙
int main() {
    int A,B,X,Y;
    cin >> A >> B >> X >> Y;
    int d = (Y - X) / (B - A);
    cout << X - (A * d) << endl;
}

アルゴ式-等差数列の和 (1)

  • 高校数学で学ぶ一般的な等差数列の和の導出からやる感じで面白かった。
    解説
  • オーバーフローを考慮してlong long型で

アルゴ式-等差数列の和 (2)

  • 項数N、初項a、公差dの等差数列の和を求める関数を以下の公式を用いて作成すれば
    // 公式:S = N * (2a + (N-1)d) / 2
    を使えば解説のような末項を使うパターンじゃなくても答えを出せる
long long func(long long N,long long a,long long d){
    return N * (2 * a + (N-1) * d) / 2;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?