constructive algorithms:Codeforces Round #654 (Div. 2):B. Magical Calendar

原題鏈接https://codeforces.com/problemset/problem/1371/B
在這裏插入圖片描述
當時沒做出來www,首先英語不過關,讀題讀了好長時間,關鍵是我就幹想了,對於我這種菜雞來說,這種題光靠想是不行的,必須大量枚舉簡單的測試用例,找到規律然後就可以秒殺掉。
簡單說一下題意:就是找到滿足以下條件的圖形的數量:

  1. all of the painted cells to be connected by side:任意方格必須有一條邊與其他方格相鄰。
  2. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day’s cell is the leftmost cell in the next (under) row.意思是就和我們用的日曆一樣,先從左到右排(起點可以是任意位置)達到最右端後便從下一行的最左端開始。
  3. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar’s sides.意思是通過平移可以重合的圖形等效爲一種
    下面詳細說這道題怎麼找規律
    1.先找案例的規律
    input
    5
    3 4
    3 2
    3 1
    13 7
    1010000 9999999
    output
    4
    3
    1
    28
    510049495001
    在這裏插入圖片描述
    通過這一族測試用例,我們首先發現隨着r的增大,一開始ans是增大的,但在臨界點之後ans不在變化,本例中的臨界點是3,r>=3的結果是一樣的
    之後我們可以推斷出在臨界點之後的結果是取決於n的
    可是具體的規律是什麼呢?
    別急,我們再找一組規律
    在這裏插入圖片描述
    這次我們明顯可以看出,有等差數列求和的意思,當r大於臨界點後,ans=n*(n-1)/2+1;
    而在臨界點之前,ans=(1+r)*r/2;
    於是代碼出來了:
#include <iostream>

using namespace std;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
    long long n,r;
    cin>>n>>r;
    cout<<min(n*(n-1)/2+1,(1+r)*r/2)<<endl;
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章