這里寫自定義目錄標題
- A. X Axis
- 題意:
- 題解:
- 代碼:
- B. Matrix Stabilization
- 題意:
- 題解:
- 代碼:
- C. Update Queries
- 題意:
- 題解:
- 代碼:
- D. Mathematical Problem
- 題意:
- 題解:
- 代碼:
A. X Axis
題意:
你得到了三個點 x 1 x_{1} x1?, x 2 x_{2} x2?, x 3 x_{3} x3?和 你需要求出一個點使得 x 1 x_{1} x1?, x 2 x_{2} x2?, x 3 x_{3} x3?與這個點的距離和最小。
題解:
暴力枚舉各個點。
代碼:
#include<iostream>
#include<algorithm>
#include<vector>
#define int long longusing namespace std;void solve(){int x,y,z;cin>>x>>y>>z;int ans=0x3f3f3f3f;int l=min(min(x,y),z);int r=max(max(x,y),z);for(int i=l;i<=r;i++) ans=min(ans,abs(x-i)+abs(y-i)+abs(z-i));cout<<ans<<endl;
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}
B. Matrix Stabilization
題意:
給你一個大小為 n × m n \times m n×m 的矩陣,其中行的編號從上到下為 1 1 1 至 n n n ,列的編號從左到右為 1 1 1 至 m m m 。位于 i i i -th 行和 j j j -th 列交點上的元素用 a i j a_{ij} aij? 表示。
考慮穩定矩陣 a a a 的算法:
- 找到單元格 ( i , j ) (i, j) (i,j) ,使其值嚴格大于所有相鄰單元格的值。如果沒有這樣的單元格,則終止算法。如果有多個這樣的單元格,則選擇 i i i 值最小的單元格;如果仍有多個單元格,則選擇 j j j 值最小的單元格。
- 設置 a i j = a i j ? 1 a_{ij} = a_{ij} - 1 aij?=aij??1 。
- 轉到步驟 1 1 1 。
在這個問題中,如果單元格 ( a , b ) (a, b) (a,b) 和 ( c , d ) (c, d) (c,d) 有一個共同的邊,即 ∣ a ? c ∣ + ∣ b ? d ∣ = 1 |a - c| + |b - d| = 1 ∣a?c∣+∣b?d∣=1 ,那么這兩個單元格就是鄰居。
您的任務是在執行穩定算法后輸出矩陣 a a a 。可以證明這種算法不可能運行無限次迭代。
給你一個大小為 n ? m n * m n?m 的矩陣,其中行的編號從上到下為 1 1 1 至 n n n ,列的編號從左到右為 1 1 1 至 m m m 。位于 i i i -行和 j j j -列交叉處的元素用 a _ i j a\_{ij} a_ij 表示。考慮穩定矩陣 a a a 的算法:1.找到單元格 ( i , j ) (i, j) (i,j) 使其值嚴格大于所有相鄰單元格的值。如果沒有這樣的單元格,則終止算法。如果有多個這樣的單元格,則選擇 i i i 值最小的單元格;如果仍有多個單元格,則選擇 j j j 值最小的單元格。2.設置 a i , j = a i , j ? 1 a_{i,j} = a_{i,j} - 1 ai,j?=ai,j??1 。3.轉到步驟 1 1 1 .在此問題中,如果單元格 ( a , b ) (a, b) (a,b) 和 ( c , d ) (c, d) (c,d) 有一個公共邊,即 ∣ a ? c ∣ + ∣ b ? d ∣ = 1 |a - c| + |b - d| = 1 ∣a?c∣+∣b?d∣=1 ,則這兩個單元格被視為相鄰單元格。您的任務是在執行穩定算法后輸出矩陣 a a a 。可以證明這種算法不可能運行無限次迭代。
題解:
從左上角枚舉各個點,判斷當前點是否需要修改,若需要則將其修改為相鄰點的最大值即可。
代碼:
#include<iostream>
#include<algorithm>
#include<vector>
#define int long longusing namespace std;const int N=110;
int a[N][N];void solve(){int n,m;cin>>n>>m;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) cin>>a[i][j];for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){int temp=-0x3f3f3f3f;int cnt0=0;int cnt1=0;if(i-1>=1){cnt0++;temp=max(temp,a[i-1][j]);if(a[i-1][j]<a[i][j]) cnt1++;}if(i+1<=n){cnt0++;temp=max(temp,a[i+1][j]);if(a[i+1][j]<a[i][j]) cnt1++;}if(j-1>=1){cnt0++;temp=max(temp,a[i][j-1]);if(a[i][j-1]<a[i][j]) cnt1++;}if(j+1<=m){cnt0++;temp=max(temp,a[i][j+1]);if(a[i][j+1]<a[i][j]) cnt1++;}if(cnt0==cnt1) a[i][j]=temp;}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) cout<<a[i][j]<<" ";cout<<endl;}
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}
C. Update Queries
題意:
給定一個長度為n的字符串 s s s和長度為 m m m的 i n d ind ind數組,以及字符串 c c c,你可以任意安排 i n d ind ind數組和 c c c字符串的順序,然后有m次操作,第i次操作,可以將 s i n d i s_{ind_i} sindi??的值更改為 c i c_{i} ci?,問m次操作后,要求經過 m m m 次操作后的字符串 s s s 的字典序最小,輸出這個 s s s。
題解:
將 c c c 和 i n d ind ind排序,遍歷 i n d ind ind數組, c c c的首元素代替 s s s中的元素,經過m次替換就是最小的字典序。
代碼:
#include<iostream>
#include<algorithm>
#include<vector>#define int long longusing namespace std;void solve()
{int n;cin >> n;string s;cin >> s;int mask[n];for(int i = 0 ; i < n ; i ++){mask[i] = s[i] - '0';} int ans = 101010;for(int i = 1 ; i < n ; i ++){vector<int>tmp;int tot = 0;for(int j = 0 ; j < n ; j ++){if(j == i - 1){int x = mask[j] * 10 + mask[j + 1];tmp.push_back(x);j++;}else{tmp.push_back(mask[j]);}}for(auto it : tmp){cout<<it<<" ";if(it == 0){cout << 0 << endl;return;}if(it == 1){continue;}else{tot += it;}}cout<<endl;if(tot == 0) tot++;ans = min(ans , tot);}cout << ans << endl;
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}
D. Mathematical Problem
題意:
給定一個長度為n的全為數字的字符串,你可以在字符串中添加n-2個運算符( × × ×或者 + + +),使得字符串變為一個合法表達式,求出該表達式最小的結果。
題解:
長度為 2 2 2 時,輸出取出前導零的原數組即可,當長度大于 2 2 2 時,原字符串可以分為 n ? 1 n-1 n?1 個數字,然后需要 n ? 2 n-2 n?2 個運算符將其連接,因此有一個數字是兩位數,其他的均為 1 1 1 位數,要使得最后的結果最小,因此當其中一個數字為 1 1 1 的時候運算符為乘法,其他的均為加法。
代碼:
#include<iostream>
#include<algorithm>
#include<vector>#define int long longusing namespace std;void solve()
{int n;cin >> n;string s;cin >> s;int mask[n];for(int i = 0 ; i < n ; i ++){mask[i] = s[i] - '0';} int ans = 101010;for(int i = 1 ; i < n ; i ++){vector<int>tmp;int tot = 0;for(int j = 0 ; j < n ; j ++){if(j == i - 1){int x = mask[j] * 10 + mask[j + 1];tmp.push_back(x);j++;}else{tmp.push_back(mask[j]);}}for(auto it : tmp){cout<<it<<" ";if(it == 0){cout << 0 << endl;return;}if(it == 1){continue;}else{tot += it;}}cout<<endl;if(tot == 0) tot++;ans = min(ans , tot);}cout << ans << endl;
}
signed main(){int t;cin>>t;while(t--) solve();return 0;
}