Codeforces Round #742 (Div. 2) AB
2021/9/24 23:15:27
本文主要是介绍Codeforces Round #742 (Div. 2) AB,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Balanced Substring
You are given a string ss, consisting of nn letters, each letter is either ‘a’ or ‘b’. The letters in the string are numbered from 11 to nn.
s[l;r]s[l;r] is a continuous substring of letters from index ll to rr of the string inclusive.
A string is called balanced if the number of letters ‘a’ in it is equal to the number of letters ‘b’. For example, strings “baba” and “aabbab” are balanced and strings “aaab” and “b” are not.
Find any non-empty balanced substring s[l;r]s[l;r] of string ss. Print its ll and rr (1≤l≤r≤n1≤l≤r≤n). If there is no such substring, then print −1−1 −1−1.
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of testcases.
Then the descriptions of tt testcases follow.
The first line of the testcase contains a single integer nn (1≤n≤501≤n≤50) — the length of the string.
The second line of the testcase contains a string ss, consisting of nn letters, each letter is either ‘a’ or ‘b’.
Output
For each testcase print two integers. If there exists a non-empty balanced substring s[l;r]s[l;r], then print ll rr (1≤l≤r≤n1≤l≤r≤n). Otherwise, print −1−1 −1−1.
Example
Input
4 1 a 6 abbaba 6 abbaba 9 babbabbaa
Output
-1 -1 1 6 3 6 2 5
Note
In the first testcase there are no non-empty balanced subtrings.
In the second and third testcases there are multiple balanced substrings, including the entire string “abbaba” and substring “baba”.
解释与代码
其实就是判断相邻的有没有不同,有不同就输出下标
#include <cstdio> #include <cstring> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <iostream> #include <sstream> #include <set> #include <map> #include <queue> #include <bitset> #include <vector> #include <limits.h> #include <assert.h> #include <functional> #include <numeric> #include <ctime> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define ppb pop_back #define lbnd lower_bound #define ubnd upper_bound #define endl '\n' #define trav(a, x) for(auto& a : x) #define all(a) (a).begin(),(a).end() #define F first #define S second #define sz(x) (ll)x.size() #define hell 1000000007 #define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl; #define display(x) trav(a,x) cout<<a<<" ";cout<<endl; #define what_is(x) cerr << #x << " is " << x << endl; #define ini(a) memset(a,0,sizeof(a)) #define case ll T;read(T);for(ll Q=1;Q<=T;Q++) #define lowbit(x) x&(-x) #define pr printf #define sc scanf #define _ 0 #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define DBG(x) \ (void)(cout << "L" << __LINE__ \ << ": " << #x << " = " << (x) << '\n') #define TIE \ cin.tie(0);cout.tie(0);\ ios::sync_with_stdio(false); //#define long long int //using namespace __gnu_pbds; template <typename T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getchar(); } x *= f; return; } inline void write(long long x) { if(x<0) putchar('-'), x=-x; if(x>9) write(x/10); putchar(x%10+'0'); putchar('\n'); } using namespace std; typedef long long ll; typedef unsigned long long ull; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const int maxn = 100909; const ll N = 5; char ch[10009]; int a, b; void solve(){ int n; cin>>n; getchar(); gets(ch+1); if (n==1) { cout<<"-1 -1"<<endl; return ; } else { for (int i=1; i<=n; i++) { if (i<=n-1 && ch[i]!=ch[i+1]) { cout<<i<<" "<<i+1<<endl; return ; } } } cout<<"-1 -1"<<endl; } int main() { case{solve();} }
Chess Tournament
A chess tournament will be held soon, where nn chess players will take part. Every participant will play one game against every other participant. Each game ends in either a win for one player and a loss for another player, or a draw for both players.
Each of the players has their own expectations about the tournament, they can be one of two types:
- a player wants not to lose any game (i. e. finish the tournament with zero losses);
- a player wants to win at least one game.
You have to determine if there exists an outcome for all the matches such that all the players meet their expectations. If there are several possible outcomes, print any of them. If there are none, report that it’s impossible.
Input
The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases.
The first line of each test case contains one integer nn (2≤n≤502≤n≤50) — the number of chess players.
The second line contains the string ss (|s|=n|s|=n; si∈{1,2}si∈{1,2}). If si=1si=1, then the ii-th player has expectations of the first type, otherwise of the second type.
Output
For each test case, print the answer in the following format:
In the first line, print NO if it is impossible to meet the expectations of all players.
Otherwise, print YES, and the matrix of size n×nn×n in the next nn lines.
The matrix element in the ii-th row and jj-th column should be equal to:
- +, if the ii-th player won in a game against the jj-th player;
- -, if the ii-th player lost in a game against the jj-th player;
- =, if the ii-th and jj-th players’ game resulted in a draw;
- X, if i=ji=j.
Example
Input
3 3 111 2 21 4 2122
Output
YES X== =X= ==X NO YES X--+ +X++ +-X- --+X
解释与代码
我是把数据构造出来但不输出,i=j
的情况就是X
,1的情况就是=
2的情况就是先判断它ij
和ji
是不是都是空的,都是空的就+
最后反转过来
特殊情况就是2只有1个和2个的情况不行,我就是少了2个的情况
#include <cstdio> #include <cstring> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <iostream> #include <sstream> #include <set> #include <map> #include <queue> #include <bitset> #include <vector> #include <limits.h> #include <assert.h> #include <functional> #include <numeric> #include <ctime> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define ppb pop_back #define lbnd lower_bound #define ubnd upper_bound #define endl '\n' #define trav(a, x) for(auto& a : x) #define all(a) (a).begin(),(a).end() #define F first #define S second #define sz(x) (ll)x.size() #define hell 1000000007 #define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl; #define display(x) trav(a,x) cout<<a<<" ";cout<<endl; #define what_is(x) cerr << #x << " is " << x << endl; #define ini(a) memset(a,0,sizeof(a)) #define case ll T;read(T);for(ll Q=1;Q<=T;Q++) #define lowbit(x) x&(-x) #define pr printf #define sc scanf #define _ 0 #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define DBG(x) \ (void)(cout << "L" << __LINE__ \ << ": " << #x << " = " << (x) << '\n') #define TIE \ cin.tie(0);cout.tie(0);\ ios::sync_with_stdio(false); //#define long long int //using namespace __gnu_pbds; template <typename T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch ^ 48); ch = getchar(); } x *= f; return; } inline void write(long long x) { if(x<0) putchar('-'), x=-x; if(x>9) write(x/10); putchar(x%10+'0'); putchar('\n'); } using namespace std; typedef long long ll; typedef unsigned long long ull; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 0x3f3f3f3f; const ll LLINF = 0x3f3f3f3f3f3f3f3f; const int maxn = 100909; const ll N = 5; int n; char ch[1009]; char cb[109][109]; int cntx[109]; int cnty[109]; void solve(){ ini(cntx); ini(cnty); int c1 = 0, c2 = 0; cin>>n; getchar(); gets(ch+1); for (int i=1; i<=n; i++) { if (ch[i] == '1') c1++; else c2++; } if (c2 == 0) { cout<<"YES"<<endl; for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { if (i == j) cout<<"X"; else cout<<"="; } cout<<endl; } } else if (c2 == 1 || c2 == 2){ cout<<"NO"<<endl; } else { cout<<"YES"<<endl; for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { if (i == j) { cb[i][j] = 'X'; } else cb[i][j] = '0'; } } int ccc = 0; for (int i=1; i<=n; i++) { if (ch[i] == '2') for (int j=1; j<=n; j++) { if (ch[j] == '1') continue; if (ch[j] == '2' && cb[i][j] == '0' && cb[j][i] == '0') { cb[i][j] = '+'; cntx[i]++; break; } } } for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { if (cb[i][j] == '0') { if (cb[j][i] == '+'){ cb[i][j] = '-'; } else if (cb[j][i] == '-') { cb[i][j] = '+'; }else{ cb[i][j] = '='; } } } } for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { cout<<cb[i][j]; } cout<<endl; } } } int main() { case{solve();} }
这篇关于Codeforces Round #742 (Div. 2) AB的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-12深入理解 ECMAScript 2024 新特性:Map.groupBy() 分组操作
- 2025-01-11国产医疗级心电ECG采集处理模块
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势