新一届暑期积分赛题目记录
原題位置忘了,自己造的數據,兩題通過率均不超過0.15,但是是過人最多的兩道,整體比賽出的有點失敗。。
D.最近的商店(比賽中叫 不會吧不會有人覺得這不是簽到吧)
Time limit:1 seconds
Memory limit:64 megabytes
? 協會成員馬上要入住算協小鎮了!他們不想去一個離自己很遠的商店,因此每個人都想知道離自己房子最近的商店有多遠。
? 他們向你求助,希望你能幫他們做一個標有商店距離的平面圖。
? 算協小鎮上的房子嚴格按照n?mn * mn?m的點陣建造,其中分布有居民樓和商店,距離按照每個單元格進行計算(橫與縱坐標差之和)。
輸入
? 第一行輸入nnn和mmm,隨后nnn行,每行mmm列,111代表商店,000代表居民樓。
輸出
? 輸出一個n?mn * mn?m的矩陣,代表距離商店的平面圖。(商店本身輸出000)
保證
對于所有輸入xxx(代指所有可能出現的輸入),都有1≤x≤9991 \le x \le 9991≤x≤999
測試樣例1
input
4 5 00001 00110 01100 00010output
3 2 1 1 0 2 1 0 0 1 1 0 0 1 2 2 1 1 0 1多源BFS模板題,若單個查找每個起點,其時間復雜度最大為O((n?m)2)O((n*m)^2)O((n?m)2)?,因此需要一次將所有起點加入隊列進行BFS查找。
代碼
#include<iostream> #include<algorithm> #include<string.h> #include<stdio.h> #include<string> #include<queue>using namespace std;const int goin[4][2] = { 1,0,0,1,-1,0,0,-1 }; int n, m; int num[1005][1005], vis[1005][1005], g[1005][1005]; queue<pair<int, int>>q;bool pd(int x, int y) {return (x > -1 && y > -1 && x < n&& y < m); }pair<int, int> move(int x, int y, int i) {return {(x + goin[i][0]), (y + goin[i][1])}; }void bfs() {while (!q.empty()) {auto [x, y] = q.front();q.pop();for (int i = 0; i < 4; i++){auto [x1, y1] = move(x, y, i);if (pd(x1, y1)){if(!vis[x1][y1] && num[x1][y1] == 0) {vis[x1][y1] = 1;q.emplace(x1, y1);g[x1][y1] = g[x][y] + 1;}}}} }int main() {cin >> n >> m;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%1d", &num[i][j]);if (num[i][j] == 1) q.emplace(i, j);}}bfs();for (int i = 0; i < n; i++) {cout << g[i][0];for (int j = 1; j < m; j++) {cout << ' ' << g[i][j];}cout << endl;}return 0; }G.紅燒羊排(比賽中 叫別說了別說了我真不是簽到)
Time limit:1 seconds
Memory limit:32 megabytes
? 紅燒羊排想要過河。河非常的寬,每隔一米有一個落腳點。羊排每次可以有如下行動:
? 【1】 每次可以走*111*米。
? 【2】 每次可以走上一步的兩倍距離。
? 河的那邊有陷阱,所以小羊排的最后一步必須恰好落在岸邊,現在給你河寬*nnn*,請你求出羊排最少多少步可以過河。
輸入
? 第一行輸入ttt,代表ttt個測試樣例。
? 之后t行,每行輸入一個nnn,代表河寬。
輸出
? 對于每一行nnn,輸出一個結果。
保證
1≤t≤1e61 \le t \le 1e61≤t≤1e6
1≤n≤1e181 \le n \le 1e181≤n≤1e18
測試樣例1
input
10 1 2 3 4 5 6 7 8 9 10output
1 2 2 3 4 4 3 4 5 5測試樣例2
input
6 1022 1023 1024 2046 2047 2048output
18 10 11 20 11 12? 本題使用貪心的思想,每次走盡可能大的步數,若這一步的兩倍要多于剩余距離,再從1開始走。
? 數據量比較大,極限情況下若每走一步一計算,復雜度約為1e91e91e9乘一個常數,因此需要提前打表。
? 因每一次是上一步的兩倍,因此跳躍一次可當作2n?12 ^ n -12n?1 ,此時nnn為這一次跳躍的步數。因(2n?1)?2<2n?2?1(2 ^ n -1) * 2 < 2 ^ n * 2 - 1(2n?1)?2<2n?2?1,每次跳躍的距離需要判斷兩次。
代碼
#include<iostream> #include<algorithm> #include<stdio.h> #include<string> #include<string.h> typedef long long ll;using namespace std;int t, i, ans; ll n, sic[70];int main() {sic[0]=1; for(i = 1; sic[i - 1] <= 1e18 / 2; i++){sic[i] = sic[i-1] * 2;}cin >> t;while(t--){scanf("%lld", &n);int j = i - 1; ans = 0;for( ; n && j > 0; j--){if(n >= sic[j] - 1){n -= (sic[j] - 1);ans += j;j++;}}printf("%d\n", ans);}return 0; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的新一届暑期积分赛题目记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方舟翼龙吃什么(方舟生存进化)
- 下一篇: 网络流-EK求最大流