本文最后更新于 844 天前,其中的信息可能已经有所发展或是发生改变。
原题链接
描述:
众嗦粥汁,HF 最近天天泡在实验室里做他的智能小车车,但在调试的时候发现控制转向和行进的指令搞混了。这种小事对他来说太简单了,用他的原话说就是:"有手就行",于是他就懒得继续做下去了。
HF 把这个做了一半的车子丢给了 LYS,如果他能解决掉这些问题,这个智能小车车就归 LYS 辣。
以下是需要解决的问题:
- 当输入右转向指令为
R
时,执行左转指令 L
,反之亦然。
- 当输入前进指令为
U
时,执行向后退指令 D
,反之亦然。
LYS 肥肠想要这个小车车,可是他啥也不会,现在他来找你帮忙了,你能帮帮他吗?
输入格式:
一行,一个字符 P,表示输入的指令。
输出格式:
一行,一个字符,表示执行的指令。
数据范围:
P=R,L,U,D。
样例输入:
样例输出:
思想:
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| void solve(){ |
| |
| char op; cin >> op; |
| if(op == 'R') cout << 'L' << endl; |
| else if(op == 'L') cout << 'R' << endl; |
| else if(op == 'U') cout << 'D' << endl; |
| else cout << 'U' << endl; |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
野兽仙贝想跟你 Van 一个游戏,要是你赢了就能让你嘿嘿嘿,反之亦然。
野兽仙贝拿出了三个超大号皮搋子放在桌上,分别编号为 1,2,3,初始对应位置编号也为 1,2,3。其中某个的皮搋子下面藏着一个扩音器。
游戏规则:
输入格式:
第一行,一个整数 k,代表游戏进行几轮。
接下来 k 行,每行输入两个整数 ni,mi 和一个整数 Pi,分别代表皮搋子的编号和你所猜的扩音器的位置编号。
输出格式:
一行,一个整数,表示你可能得到的最高分数。
数据范围:
1≤k≤100,1≤ni,mi,Pi≤3。
输入样例:
输出样例:
提示:
若扩音器最开始在皮搋子 1 下面,最终你在第三轮猜中一次。
若扩音器最开始在皮搋子 2 下面,最终你在第一轮和第二轮猜中,共两次。
若扩音器最开始在皮搋子 3 下面,最终一次也没有猜中。
思想1:
- 暴力模拟。
- 枚举初始位置,模拟在该开局的条件下的得分。
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| int pos[N]; |
| |
| struct Operator{ |
| int l, r, p; |
| }op[N]; |
| |
| void solve(){ |
| |
| int n; cin >> n; |
| |
| for(int i = 1; i <= n; i ++){ |
| int a, b, c; cin >> a >> b >> c; |
| op[i] = {a, b, c}; |
| } |
| |
| int ans = 0; |
| |
| for(int i = 1; i <= 3; i ++){ |
| pos[1] = pos[2] = pos[3] = 0; |
| pos[i] = 1; |
| int cnt = 0; |
| for(int j = 1; j <= n; j ++){ |
| swap(pos[op[j].l], pos[op[j].r]); |
| cnt += pos[op[j].p]; |
| } |
| ans = max(ans, cnt); |
| } |
| |
| cout << ans << endl; |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
思想2:
- 思维题。
- 设位置数组为
pos[N]
,初始时位置 i
上的皮搋子编号为 i
,即 pos[1] = 1
表示位置 1 上的皮搋子编号为 1。
- 设答案数组为
ans[N]
,表示编号为 i
的皮搋子被猜中多少次。
- 用
swap()
交换位置上的皮搋子,而询问的位置上的皮搋子对应的编号被猜中,记录次数。
- 最后比较哪个编号的皮搋子在交换时被猜中的次数最多,那么一开始扩音器就在这个皮搋子下。
代码2:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| int ans[N], pos[N]; |
| |
| void solve(){ |
| |
| int n; cin >> n; |
| |
| for(int i = 1; i <= 3; i ++) pos[i] = i; |
| |
| for(int i = 0; i < n; i ++){ |
| int a, b, c; cin >> a >> b >> c; |
| swap(pos[a], pos[b]); |
| ans[pos[c]] ++; |
| } |
| |
| cout << max(ans[1], max(ans[2], ans[3])) << endl; |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
三角形给人一种稳固的感觉,我们的好姐姐 WJX 最喜欢三角形了,现在她想用一些字母拼出来等腰三角形,你能帮帮她吗?
输入格式:
第一行,一个整数 t,表示样例数量。
接下来 t 行,第 ti 行输入一个整数 ni 和一个字符 ci,分别表示三角形的高度和用来拼成三角形的字符。
输出格式:
共 t 行,每行用所给字符输出对应高度的等腰三角形。
每个样例之间空一行。
数据范围:
1≤t,ni≤10。
样例输入:
样例输出:
思想:
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1804; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| void solve(){ |
| |
| int n; char op; |
| cin >> n >> op; |
| |
| for(int i = 1; i <= n; i ++){ |
| if(i <= n - 1){ |
| for(int j = n - i - 1; j >= 0; j --) cout << ' '; |
| cout << op; |
| for(int j = 1; j <= (i - 1) * 2 - 1; j ++) cout << ' '; |
| if(i > 1) cout << op << endl; |
| else cout << endl; |
| } |
| else{ |
| for(int j = 1; j <= i * 2 - 1; j ++) cout << op; |
| cout << endl; |
| } |
| } |
| |
| cout << endl; |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| cin >> _; |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
我们的好姐姐 WJX 在拼三角形的时候突发奇想,想要考考小陈同学,假如有 n 个整数 i,以这 n 个整数中的三个整数为边长能够构成多少个三角形?
结果小陈同学一时大意,不小心被难住了,你能帮帮他吗?
输入格式:
第一行一个整数 t ,表示 t 个测试样例。
每个样例第一行一个整数 n,表示有 n 个可选的边
每个样例第二行 n个整数 i,表示三角形的边长。
输出格式:
共 t 行,每行一个整数,表示该测试样例下能构成的三角形的数量。
数据范围:
1≤t,n,i,≤1×103。
样例输入:
样例输出:
思想:
- 排序,二分。
- 将所有的边按照从小到大的顺序排序。
- 枚举前两个边的长度,二分寻找最大的第三边的位置。
- 将满足的第三边的总数累加即可。
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| int a[N]; |
| |
| void solve(){ |
| |
| int n; cin >> n; |
| for(int i = 0; i < n; i ++) cin >> a[i]; |
| |
| sort(a, a + n); |
| |
| LL sum = 0; |
| |
| for(int i = 0; i + 2 < n; i ++){ |
| while(a[i] == 0 && i < n) i ++; |
| for(int j = i + 1; j + 1 < n; j ++){ |
| int l = j + 1, r = n - 1; |
| int p = a[i] + a[j]; |
| while(l < r){ |
| int mid = l + r + 1 >> 1; |
| if(a[mid] < p) l = mid; |
| else r = mid - 1; |
| } |
| if(a[l] < p) sum += l - j; |
| else sum += l - j - 1; |
| } |
| } |
| |
| cout << sum << endl; |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| cin >> _; |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
小陈同学很感谢你的帮助,作为回报,他也给你出了一个有趣的问题。
现在给出一个“卷点”的定义:在一个正方形内存在某一点,使得这个点和正方形四个顶点连接而划分出的四块三角形的面积比为 a:b:c:d。四个数不分顺序;卷点不是唯一的。
现在有一个正方形,给出 4个整数 a,b,c,d,表示被某点划分出的四个三角形的面积,判断有多少个这样的卷点在正方形内部?
输入格式:
第一行一个整数 n ,代表 n 个测试样例。
接下来 n 行,每行 4 个整数 a,b,c,d,表示四个三角形的面积。
输出格式:
共 n 行,每行一个整数,表示卷点的数量。
数据范围:
1≤n≤1×102,1≤a,b,c,d≤1×1018。
样例输入:
样例输出:
思想:
- 思维题。
- 设 a,b,c,d 非严格单调递增。
- 则当 a+d=b+c 时:
- 若 a=b=c=d 时,则只有一个卷点;
- 若 a=b,a=d 时,则存在 4 个卷点;
- 若 a,b,c,d 均不相等时,则存在 8 个卷点。
- 其他情况不存在卷点。
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| LL a[N]; |
| |
| void solve(){ |
| |
| for(int i = 0; i < 4; i ++) cin >> a[i]; |
| sort(a, a + 4); |
| if(a[0] + a[3] == a[1] + a[2]){ |
| if(a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) cout << 1 << endl; |
| else if(a[0] == a[1] && a[1] != a[2]) cout << 4 << endl; |
| else cout << 8 << endl; |
| } |
| else cout << 0 << endl; |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| cin >> _; |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
给定一个字符串 S ,和两个整数 N,M,(0≤N≤M≤9),按照出现的顺序输出其中所有的在 N∼M 之间的数字字符。
输入格式:
第一行,两个整数 N,M。
第二行,一个字符串 S。
输出格式:
一行,若干个整数,为 S 处理后的结果,每个整数间隔一个空格。
数据范围:
保证 S 的长度 L(S) 满足 1≤L(S)≤1×105。
样例输入:
样例输出:
思想:
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1804; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| void solve(){ |
| |
| int n, m; string s; |
| cin >> n >> m >> s; |
| |
| for(int i = 0; i < s.size(); i ++){ |
| int t = s[i] - '0'; |
| if(t >= n && t <= m) cout << s[i] << ' '; |
| } |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
LYS 最喜欢摸鱼了, 现在给出一个长度为 N 的序列 A=A1,A2,…,AN,表示 LYS 的日程表,其中 Ai 表示第 i 天的摸鱼指数。
为了在排满的日程表里的摸鱼,他向摸鱼大师 HF 请教,摸鱼大师给出了一个摸鱼公式:
-
Ai1−AiAjj=Aj1−AiAji,(i<j)
由公式可知,当第 i 天和第 j 天及其对应的摸鱼指数满足上述公式时,他就可以在第 i 天和第 j 天这两天摸鱼,记为一种摸鱼方案 (i,j)。
虽然只能摸两天的鱼,但是 LYS 还是想知道,他的日程表有多少种摸鱼方案可选。
输入格式:
第一行,一个整数 N,代表天数。
接下来 N 个整数,代表第 i 天的摸鱼指数 Ai。
输出格式:
一行,一个整数,表示方案总数。
数据范围:
1≤N≤1×106,1≤Ai≤1×103。
输入样例:
输出样例:
提示:
对于样例,只有 (4,6) 符合。
思想:
- 思维。
- 化简 Ai1−AiAjj=Aj1−AiAji,(i<j) 得 Aj−j=Ai−i。
- 由此,不妨设 Bi=Ai−i。
- 总方案数即求对于 j 之前的与 Bj 值相等的 Bi 数量之和。
- 记录已经出现过的 Bi 数量,之后出现的累加即可。
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| void solve(){ |
| |
| LL sum = 0; |
| |
| map<int,int> vis; |
| |
| int n; cin >> n; |
| for(int i = 0; i < n; i ++){ |
| int x; cin >> x; |
| x -= i; |
| sum += vis[x]; |
| vis[x] ++; |
| } |
| |
| cout << sum << endl; |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |
原题链接
描述:
在习得摸鱼大师 HF 给出的公式之后,LYS 发现 HF 给的摸鱼公式只适用于地球上的日程表,于是他提出了“宇宙摸鱼大一统理论”,以便适应全宇宙不同摸鱼人群的需求。
现在给出一个长度为 N 的序列 A=A1,A2,…,AN,表示某一日程表,其中 Ai 表示第 i 天的摸鱼指数。
LYS 提出的理论如下:
输入格式:
第一行,一个整数 N,代表天数。
接下来 N 个整数,代表第 i 天的摸鱼指数 Ai。
输出格式:
一行,一个整数,表示某两天满足摸鱼条件的时候 i+j 的最大值。
数据范围:
1≤N≤1×106,1≤Ai≤1×103。
输入样例:
输出样例:
提示:
互质:当 A 和 B 的最大公约数为 1 时,称 A 和 B 互质。
对于样例,在 i=5,j=6 时满足摸鱼条件,且摸鱼质量最高。
思想:
- 离散化,最大公约数。
- 互质的条件为 gcd(Ai,Aj)=1。
- 对于相同的 Ai,我们记录其所在的下标的最大值即可,不必维护所有 Ai 的下标。
- 即将 A 序列去重,但保留 Ai 原有的最大下标。
- 枚举离散化存储的 Ai 判断即可,满足摸鱼的条件则维护 i+j 的最大值。
代码:
| #include <iostream> |
| #include <cstring> |
| #include <cstdio> |
| #include <algorithm> |
| #include <cmath> |
| #include <sstream> |
| #include <vector> |
| #include <queue> |
| #include <stack> |
| #include <map> |
| #include <set> |
| #include <unordered_map> |
| #include <unordered_set> |
| |
| using namespace std; |
| |
| #define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr) |
| #define re register |
| #define fi first |
| #define se second |
| #define endl '\n' |
| |
| typedef long long LL; |
| typedef pair<int, int> PII; |
| typedef pair<LL, LL> PLL; |
| |
| const int N = 1e6 + 3; |
| const int INF = 0x3f3f3f3f, mod = 1e9 + 7; |
| const double eps = 1e-6, PI = acos(-1); |
| |
| int gcd(int a, int b){ |
| return b ? gcd(b, a % b) : a; |
| } |
| |
| void solve(){ |
| |
| int res = -1; |
| |
| int n; cin >> n; |
| |
| map<int, int> a; |
| |
| for(re int i = 1; i <= n; i ++){ |
| int x; cin >> x; |
| a[x] = i; |
| } |
| |
| for(auto &p : a){ |
| for(auto &t : a){ |
| if(gcd(p.fi, t.fi) == 1) res = max(res, p.se + t.se); |
| } |
| } |
| |
| cout << res << endl; |
| |
| } |
| |
| int main(){ |
| |
| IOS; |
| |
| int _ = 1; |
| |
| |
| |
| while(_ --){ |
| solve(); |
| } |
| |
| return 0; |
| |
| } |