1. 程式人生 > >AtCoder Beginner Contest 099 完整解題報告

AtCoder Beginner Contest 099 完整解題報告

題目連結

A題

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    cout << (n < 1000 ? "ABC" : "ABD") << endl;

    return 0;
}

B題

s1 = 1
s2 = 1 + 2
s3 = 1 + 2 + 3
sn = 1 + 2 + 3 + …… + n
對於相鄰的兩個塔來說,無論積雪多少米,b-a都是第2個塔的n。求出n之後,再根據求和公式s = n * (n + 1) / 2,即可求出塔高

#include <cstdio>
using namespace std;

int a,b,now;

int main()
{
    scanf("%d%d",&a,&b);
    now=b-a;    //算出b等差數列的最後一項
    printf("%d",(now+1)*now/2-b);     //(now+1)*now/2為b塔的原始高度(等差數列求和公式)
    return 0;
}

C題

注意,本題不能使用貪心演算法

#include <iostream>
#include <cmath>

using
namespace std; int main() { int n; cin >> n; int res = n; for(int i = 0; i <= n; i++) { int cnt = 0; int tmp = i; while(tmp > 0) { cnt += tmp % 6; tmp /= 6; } tmp = n - i; while(tmp > 0) { cnt += tmp % 9
; tmp /= 9; } if(res > cnt) { res = cnt; } } cout << res << endl; return 0; }

D題

對於二維網格C(i, j),定義好網格:
若座標%3的值相同,則顏色也相同。比如C(1, 2) = C(2, 1)
若座標%3的值不相同,則顏色也不相同。比如C(1, 1) != C(1,2) != C(2,2)

D為代價值, 假如C(1, 2) = 2, 要調整為C(1, 2) = 3,則代價值為D(2, 3)
再比如C(2, 1) = 3,要調整為C(2, 1) = 2,則代價值為D(3, 2)
再比如C(1, 2) = 1, 要調整為C(1, 2) = 1,則代價值為D(1, 1)。這個其實不用調整,所以D(1, 1) = 0,同理D(x, x) = 0

用第1個例子來計算,

D = 
0 1 1
1 0 1
1 4 0

C = 
1 2
3 3

這裡C(1,1)的座標模3等於(1 + 1) % 3 = 2
C(1, 2)的座標模3等於(1 + 2) % 3 = 0
C(1, 2)的座標模3等於(2 + 1) % 3 = 0
C(2, 2)的座標模3等於(2 + 2) % 3 = 1
題目要求C(1, 2) = C(2, 1),C(1,1)、C(2,1)和C(2,2)要兩兩互不相等。

可以用窮舉法調整出6種好網格:
(一)
C(1, 1) = 1, D(1, 1) = 0
C(1, 2) = 2, D(2, 2) = 0
C(2, 1) = 2, D(3, 2) = 4
C(2, 2) = 3, D(3, 3) = 0
D = D(1, 1) + D(2, 2) + D(3, 2) + D(3, 3) = 4

(二)
C(1, 1) = 1, D(1, 1) = 0
C(1, 2) = 3, D(2, 3) = 1
C(2, 1) = 3, D(3, 3) = 0
C(2, 2) = 2, D(3, 2) = 4
D = 0 + 1 + 0 + 4 = 5

(三)
C(1, 1) = 2, D(1, 2) = 1
C(1, 2) = 1, D(2, 1) = 1
C(2, 1) = 1, D(3, 1) = 1
C(2, 2) = 3, D(3, 3) = 0
D = 1 + 1 + 1 = 3

(四)
C(1, 1) = 2, D(1, 2) = 1
C(1, 2) = 3, D(3, 2) = 4
C(2, 1) = 3, D(3, 3) = 0
C(2, 2) = 1, D(3, 1) = 1
D = 6

(五)
C(1, 1) = 3, D(3, 1) = 1
C(1, 2) = 1, D(2, 1) = 1
C(2, 1) = 1, D(3, 1) = 1
C(2, 2) = 2, D(3, 2) = 4
D = 7

(六)
C(1, 1) = 3, D(3, 1) = 1
C(1, 2) = 2, D(2, 2) = 0
C(2, 1) = 2, D(3, 2) = 4
C(2, 2) = 1, D(3, 1) = 1
D = 6

這六種調整方法中,最小的D為3,所以本例子的答案為6

本題的難點主要是理解題意,解題倒不難,用窮舉法即可。
程式碼:

// Algorithm: Brute-force

#include "cstdio"
using namespace std;

const int maxn=30+10;
int n,c,d[maxn][maxn],t[4][maxn],x,y,z,ans;

int main()
{
    ans=0x7fffffff;    // 先把答案記為一個極大值,出現更小的就更新
    scanf("%d%d",&n,&c);

    register int i,j,k,l;
    for(i=1;i<=c;i++)
    {
        // 輸入D矩陣(i-j)為顏色i變為顏色j的代價為(i,j)
        for(j=1;j<=c;j++)
        {
            scanf("%d",&d[i][j]);
        }
    }

    for(i=1;i<=n;i++)
    {
        // 輸入矩陣C
        for(j=1;j<=n;j++)
        {
            scanf("%d",&x);
            t[(i+j)%3][x]++;    // 計算橫座標加縱座標mod3的數值,顏色為x的個數+1
        }
    }

    for(i=1;i<=c;i++)
    {
        // 列舉座標餘數為0的顏色,統一變成顏色i,其代價之和為x
        // 比如例1中,座標為1行2列的餘數為(1+2)%3=0,其值2,變成i=1則D_21=1
        // 座標2列1行的餘數為(2+1)%3=0,其值為3,變成i=1則有D_31=31
        // x = D_21 + D_31 = 2
        x=0;
        for(j=1;j<=c;j++)
        {
            x+=t[0][j]*d[j][i];
        }

        for(j=1;j<=c;j++)
        {    // 列舉餘數為1的顏色,統一變成顏色j,其代價之和為y
            if(j==i)
            {
                continue;    // 不同餘數的顏色不能相同
            }

            y=0;
            for(k=1;k<=c;k++)
            {
                y+=t[1][k]*d[k][j];
            }

            for(k=1;k<=c;k++)
            {
                // 列舉座標餘數為2的顏色,統一變成顏色k,其代價之和為z
                if(k==j || k==i)
                {
                    continue;    // 不同餘數的顏色不能相同
                }

                z=0;
                for(l=1;l<=c;l++)
                {
                    z+=t[2][l]*d[l][k];
                }

                if(ans>x+y+z)
                {
                    // 如果當前轉換的代價和比之前的最優答案更小,就更新代價和
                    ans=x+y+z;
                }
            }
        }
    }

    printf("%d",ans);
    return 0;
}

TopCoder & Codeforces & AtCoder交流QQ群:648202993
更多內容請關注微信公眾號
wechat_public_header.jpg

相關推薦

AtCoder Beginner Contest 099 完整解題報告

題目連結 A題 #include <iostream> using namespace std; int main() { int n; cin >> n; cout << (n &

AtCoder Beginner Contest 117 解題報告

long long begin == ostream stream 如果 vector 排序。 include 果然abc都是手速場。 倒序開的qwq。 D題因為忘記1e12二進制幾位上界爆了一發。 A - Entrance Examination 就是除一下就行了。。。

AtCoder Beginner Contest 113 B

B - Palace Time limit : 2sec / Memory limit : 1024MB Score: 200 points Problem Statement A country decides to buil

AtCoder Beginner Contest 113 A

A - Discount Fare Time limit : 2sec / Memory limit : 1024MB Score: 100 points Problem Statement There is a train g

AtCoder Beginner Contest 113 C

C - ID Time limit : 2sec / Memory limit : 1024MB Score: 300 points Problem Statement In Republic of Atcoder, there

AtCoder Beginner Contest 109 C

C - Skip Time limit : 2sec / Memory limit : 1024MB Score : 300 points Problem Statement There are N cities on a number line. The i-th city

AtCoder Beginner Contest 113 B - Palace

Problem Statement A country decides to build a palace. In this country, the average temperature of a point at an elevation of x me

AtCoder Beginner Contest 113 A - Discount Fare

Problem Statement There is a train going from Station A to Station B that costs X yen (the currency of Japan).

AtCoder Beginner Contest 113 C - ID

C - ID Time limit : 2sec / Memory limit : 1024MB Score: 300 points Problem Statement In Republic of Atcoder, ther

AtCoderAtCoder Beginner Contest 103 (ABC103)

先上一張最終結果的圖吧: 感覺AtCoder的ABC還是比較練手的,考驗程式碼速度,網速,D題還會有一些思維難度。 這次ABC由於網路原因,很遲才看到題,但完成得還是不錯的。 題解: A 題意:給你三個都需要被完成的任務的難度,均為1至100的正整數。 首先,你可以

AtCoder Beginner Contest 114 Solution

A 753 Solved. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int mp[10]; 5 6 int main() 7

Atcoder Beginner Contest 115 D Christmas 模擬,遞迴 B

D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some other world,

AtCoder Beginner Contest 115

比賽連結 Tasks # Title Time limit Memory limit A Christmas Eve Eve Eve 2 sec 1024 MB Submit

AtCoder Beginner Contest 115 題解

most otto abc ostream def ring decide cin string 題目鏈接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 題目: Time limit : 2sec /

AtCoder Beginner Contest 115 Solution

A Christmas Eve Eve Eve Solved. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6

AtCoderAtCoder Beginner Contest 110題解

AtCoder Beginner Contest 110題解 A - Maximize the Formula 題目大意 給定三個數字A,B,CA,B,CA,B,C,要求使用這三個數字組成一個兩位數

Dire Wolf【區間DP】【完整解題報告記錄推導過程】

  思路:   這個是一個DP的題目,原因是前一刻的狀態會影響到後一刻的狀態,所以我從DP的角度來思考這道題的解法,我想的是假如【i,j】的狼已經被吃完了,那麼接下來【i-1,j+1】的狼就會受到【i,j】的影響,只要判斷【i,j+1】和【i-1,j】哪個區間段再加

AtCoder Regular Contest 099 題解

題意: 給出一個操作序列包含&lt;&gt;+−&lt;&gt;+-<>+−,分別是下標左移右移,當前位置加減。問有多少對(i,j)(i,j)(i,j)滿足只

AtCoder Beginner Contest 110

第一場ATcoder就遇到測試樣例出問題取消本場積分的事情,嗚嗚嗚~~一份題解證明我來過!ABC水過前三道,排名575. 上週的比賽,因為最後一題加上拖延症,還是終於在本週比賽開始前來寫這個解題報告了。第一場體驗還是不錯的,後來發現有題解值得表揚(雖然是日語的)! 題

(9月29日)AtCoder Beginner Contest 111

之前做的比賽,一直沒整理。當時做出來兩道題,水水的~比cf的div2要簡單 當時比賽只寫出了A,B兩題 #include<iostream> #include<cst