Educational Codeforces Round 123
比赛地址
A. Doors and Keys
简单的顺序遍历
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define PII pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repd(i, l, r) for (int i = l; i <= r; i++)
#define rep2(i, l, r) for (int i = l; i * i <= r; i++)
#define rep3(i, l, r) for (LL i = l; i * i * i <= r; i++)
#define Min(a, b) a > b ? b : a
#define Max(a, b) a > b ? a : b
#define x first
#define y second
#define endl '\n'
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, M = 1010;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
string solve(){
string s;
cin >> s;
bool r_ = 0 , g_ = 0 , b_ = 0;
rep( i , 0 , s.size() ){
if( s[i] <= 'z' && s[i] >= 'a' ) {
if( s[i] == 'r' ) r_ = 1;
else if( s[i] == 'g' ) g_ = 1;
else b_ = 1;
}
else {
if( s[i] == 'G' && !g_ ) return "NO";
if( s[i] == 'B' && !b_ ) return "NO";
if( s[i] == 'R' && !r_ ) return "NO";
}
}
return "YES";
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
cout << solve() << endl;
}
return 0;
}
B. Anti-Fibonacci Permutation
可以根据限制条件写暴搜,也可以根据规律来写。
暴搜
bool st[N];
int c[N];
int t;
int n;
bool dfs(int f)
{
if(f == n)
{
for(int i = 0; i < n; i ++)
cout << c[i] << " ";
cout << endl;
return 1;
}
for(int i = 1; i <= n; i ++)
{
if(f >= 2)
{
if(!st[i] && st[c[f - 1]] && st[c[f - 2]])
{
c[f] = i;
if(c[f - 2] + c[f - 1] != c[f])
{
st[i] = 1;
if(dfs(f + 1)) return 1;
st[i] = 0;
}
}
}
else
{
if(!st[i])
{
c[f] = i;
st[i] = 1;
if(dfs(f + 1)) return 1;
st[i] = 0;
}
}
}
return 0; // 不要忘了
}
int main(void)
{
cin >> t;
while(t --)
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
memset(st, 0, sizeof st);
memset(c, 0, sizeof c);
c[0] = i;
st[i] = 1;
dfs(1);
}
}
return 0;
}
规律
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define PII pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repd(i, l, r) for (int i = l; i <= r; i++)
#define rep2(i, l, r) for (int i = l; i * i <= r; i++)
#define rep3(i, l, r) for (LL i = l; i * i * i <= r; i++)
#define Min(a, b) a > b ? b : a
#define Max(a, b) a > b ? a : b
#define x first
#define y second
#define endl '\n'
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, M = 1010;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int a[N];
void solve(){
int n,k=0;
cin >> n;
rep( i , 1 , n+1 )
a[i] = n-k++;
rep( i , 1 , n+1 ){
k = 0;
rep( j , 1 , n+1 ){
if( i == j ) cout << 1 << ' ';
else cout << a[++k] << ' ';
}
cout << endl;
}
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
C. Increase Subarray Sums
求最大子段和最大。
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define PII pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repd(i, l, r) for (int i = l; i <= r; i++)
#define rep2(i, l, r) for (int i = l; i * i <= r; i++)
#define rep3(i, l, r) for (LL i = l; i * i * i <= r; i++)
#define Min(a, b) a > b ? b : a
#define Max(a, b) a > b ? a : b
#define endl '\n'
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, M = 1010;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int a[N],s[N];
int ans[N];
void solve(){
int n,k=0,x;
cin >> n >> x;
memset( ans , -0x3f , sizeof ans );
rep( i , 1 , n+1 ){
cin >> a[i];
s[i] = s[i-1]+a[i];
}
rep( i , 1 , n+1 ){
for( int j = 1 ; j+i-1 <= n ; j++ )
ans[i] = max( ans[i] , s[j+i-1]-s[j-1] );
// 长度为i的区间和的最大值
}
int Ans = 0;
rep( k , 0 , n+1 ){
rep( i , k , n+1 )
Ans = max( Ans , ans[i]+k*x );
cout << Ans << ' ';
}
cout << endl;
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}