Codeforces Round #719 (Div. 3)
题意:
给你一个整数n,问n中有多少个各位数字都相同的数
思路:(官方思路)
因为各位数字都相同只能是1,2,11,111,222,1111这样的数,而如果我们从1枚举i,每次让乘10再加1,i每次就会变成1,11,111,1111,我们再从1到9枚举倍数j,i和j相乘每次就得到1,2,3,···11,22,33,···111,222,333
即为各位数字都相同的数,看每次i和j相乘是否小于n,如果小于n就答案加一
虽然但是。。。枚举乘法真的感觉自己的姿势又增加了呢
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cmath> 6 #include <queue> 7 #include <vector> 8 9 using namespace std; 10 11 typedef long long LL; 12 typedef pair<int, int>PII; 13 14 int main() 15 { 16 int t; 17 cin >> t; 18 while (t--) 19 { 20 int n; 21 cin >> n; 22 23 int res = 0; 24 for (LL i = 1; i <= n; i = i * 10 + 1) 25 for (int j = 1; j <= 9; j++) 26 if (i * j <= n) 27 res++; 28 29 cout << res << endl; 30 } 31 return 0; 32 }
题意:
给你一个整数n,输出一个n阶矩阵,矩阵中每个数从1到n^2只输出一次,并且上下左右的数不能和这个数相邻,即绝对值之差大于1,即2不能和1,3相邻
思路:
直接输出类型的题,在矩阵中,由左至右,由上至下,先输出奇数,再输出偶数,而n等于2是唯一没有答案的情况
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cmath> 6 #include <queue> 7 #include <vector> 8 9 using namespace std; 10 11 typedef long long LL; 12 typedef pair<int, int>PII; 13 14 int main() 15 { 16 int t; 17 cin >> t; 18 while (t--) 19 { 20 int n; 21 cin >> n; 22 23 if (n == 1) cout << 1 << endl; 24 else if (n == 2) cout << -1 << endl; 25 26 else 27 { 28 int ji = 1, ou = 2; 29 for (int i = 0; i < n; i++) 30 { 31 for (int j = 0; j < n; j++) 32 { 33 if (ji <= n * n) 34 { 35 cout << ji << ' '; 36 ji += 2; 37 } 38 else if (ou <= n * n) 39 { 40 cout << ou << ' '; 41 ou += 2; 42 } 43 } 44 cout << endl; 45 } 46 } 47 } 48 return 0; 49 }
题意:
给定一个有n个数的数组,求有多少对数字满足i < j 并且 a[j] - a[i] = j - i .
思路:(构造)
题中的条件可变成i < j 且 a[j] - j = a[i] - i, 我们令b[i] = a[i] - i, 所以我们可以开一个map存a[i] - i这个值,看他之前出现了多少次即可
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cmath> 6 #include <queue> 7 #include <vector> 8 #include <map> 9 #include <set> 10 11 using namespace std; 12 13 typedef long long LL; 14 typedef pair<int, int>PII; 15 16 int main() 17 { 18 int t; 19 cin >> t; 20 while (t--) 21 { 22 map<int, int>mp; 23 int n; 24 cin >> n; 25 LL res = 0; 26 for (int i = 0; i < n; i++) 27 { 28 int x;//a[i] 29 cin >> x; 30 res += mp[x - i]; 31 mp[x - i] ++; 32 } 33 cout << res << endl; 34 } 35 return 0; 36 }
E. Arranging The Sheep
题意:
给定一个字符串,问最少需要多少步,把所有的星号移动到都相邻。
思路:(货仓选址变形)货仓选址
只考虑如果是移动到同一个位置的话,直接每个数减去他们的中位数相加即可,现在是相邻,那我们只需要给每个位置减去它们相对于中间的数的相对位移即可。
证明:设最左边的星星走到x1=a处,那么由题意,第2个走到x2=a+1处,第i个走到xi=a+i−1处. 那么,第i个星星要走|xi−(a+i−1)|=|xi−(i−1)−a|单位距离. 将xi−(i−1)看成一个整体,问题就又转化为”货仓选址”问题了,对xi−(i−1)排序,取中位数即可.
货仓选址:在一条数轴上有 N 家商店,它们的坐标分别为 A1∼AN。
现在需要在数轴上建立一家货仓,每天清晨,从货仓到每家商店都要运送一车商品。
为了提高效率,求把货仓建在何处,可以使得货仓到每家商店的距离之和最小。
方法:1、把A[0]~A[N-1]排序,设货仓在X坐标处,X左侧的商店有P家,右侧的商店有Q家。若P < Q,则每把仓库的选址向右移动1单位距离,距离之和就会变少Q - P.同理,若P > Q,则仓库的选址向左移动会使距离之和变小。当P==Q时为最优解。
2、因此仓库应该建在中位数处,把A进行排序,
当N为奇数时,货仓建在A[(N - 1)/2]处,
当N为偶数时,仓库建在A[(N - 1)/2 + 1]处。
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cmath> 6 #include <queue> 7 #include <vector> 8 #include <map> 9 #include <set> 10 11 using namespace std; 12 13 typedef long long LL; 14 typedef pair<int, int>PII; 15 16 LL pos[1000010]; 17 18 int main() 19 { 20 int t; 21 cin >> t; 22 while (t--) 23 { 24 int n, k = 0; 25 string s; 26 cin >> n; 27 cin >> s; 28 29 for (int i = 0; i < n; i++) 30 if (s[i] == '*') 31 pos[++k] = i + 1;//记录每个星号的位置 32 33 for (int i = 1; i <= k; i++) 34 pos[i] -= i;//减去偏移量 35 36 LL res = 0; 37 for (int i = 1; i <= k; i++) 38 res += abs(pos[i] - pos[k / 2 + 1]); 39 40 cout << res << endl; 41 } 42 return 0; 43 }

浙公网安备 33010602011771号