Pinely Round 2 (Div. 1 + Div. 2)
思路: 遍历
我们直接遍历这个字符串维护两个数值,一个是全为 ‘+’ 的情况下的值,一个是既有 ‘+’ 又有 ‘-’ 的值,起初的值都为A,如果中途有一个数大于等于n就标记这个数,如果两个数都被标记了就是yes,如果两个数都没被标记就是no,否则就是maybe
代码:
#include
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=2e6+10,M=2e5;
typedef double db;
typedef pairpii;
int n,m,k,Q,cnt,t;
vectordel;
int a[200010],b[200010];
int prime[N];
bool st[N];
mapmp;
signed main(){
std::cin>>t;
while(t--){
string str;
int A,q;
cin>>n>>A>>q;
cin>>str;
int aa=A,ab=A;
if(A>=n){
cout<<"YES\n";
continue;
}
bool f1=false,f2=false;
rep(i,0,str.size()-1){
if(str[i]=='-'){
if(ab==0)aa--;
ab=max(ab-1,0LL);
}else {
aa++;
if(aa>=n)f1=true;
ab++;
if(ab>=n)f2=true;
}
}
if(f1&&f2)cout<<"YES\n";
else if(f1==false&&f2==false)cout<<"NO\n";
else cout<<"MAYBE\n";
}
}
思路:逆序问题
我们可以发现如果当前下标如果和后面的下标是逆序的就应该交换,否则就不交换,我们先标记每一个值的下标,然后遍历1~n判断他们的是不是逆序是就答案+1
#include
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=2e6+10,M=2e5;
typedef double db;
typedef pairpii;
int n,m,k,Q,cnt,t;
vectordel;
int a[200010],b[200010];
int prime[N];
bool st[N];
mapmp;
void solve(){
cin>>n;
map mp;
rep(i,1,n){
cin>>a[i];
mp[a[i]]=i;
}
int res=0;
rep(i,1,n-1){
res+=mp[i]>mp[i+1];
}
cout<>t;
while(t--)solve();
}
思路:环形问题
我觉得其实这题那个mex对帮助不大,最终会发现,其实就是将没出现的那个数组添加到末尾,再将整个数组向左移动k%(n+1)位就行了,关于环行问题我们可以用双端队列
#include
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=2e6+10,M=2e5;
typedef double db;
typedef pairpii;
int n,m,k,Q,cnt,t;
vectordel;
int a[200010],b[200010];
int prime[N];
bool st[N];
mapmp;
signed main(){
ios::sync_with_stdio(false);
cout.tie(); cin.tie();
std::cin>>t;
while(t--){
deque q;
int n, k; std::cin >> n >> k;
rep(i,0,n)st[i]=0;
rep(i,1,n) {
std::cin >> a[i];
st[a[i]] = true;
q.push_back(a[i]);
}
int s = 0;
rep(i,0,n)if (!st[i])s = i;
q.push_back(s);
k = k % (n + 1);
//cout<
思路:思维+构造
我们发现LR只对列有贡献,UD只对行有贡献,然后就是判断每一行中DU分别是不是偶数,不是就不能构成输出-1,列LR同理,然后就是构造WB,方法很多我写的是奇数个为W偶数维B
#include
using namespace std;
#define int long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
const int N=2e6+10,M=2e5;
typedef double db;
typedef pairpii;
int n,m,k,Q,cnt,t;
vectordel;
int a[200010],b[200010];
int prime[N];
bool st[N];
char g[510][510];
char f[510][510];
int l[510],r[510];
mapmp;
void solve(){
cin >> n >> m;
rep(i,0,n-1)cin >> g[i];
rep(i,0,n-1) {
int res = 0, ans = 0;
rep(j,0,m-1) {
if (g[i][j] == 'U')ans++;
else if (g[i][j] == 'D')res++;
f[i][j] = '.';
}
if (ans % 2 != 0 || res % 2 != 0) {
cout << -1 << endl;
return;
}
}
rep(i,0,m-1) {
int res = 0, ans = 0;
rep(j,0,n-1) {
if (g[j][i] == 'L')ans++;
else if (g[j][i] == 'R')res++;
}
if (ans % 2 != 0 || res % 2 != 0) {
cout << -1 << endl;
return;
}
}
rep(i,0,n-1) {
int res = 0, ans = 0;
rep(j,0,m-1) {
if (g[i][j] == 'U')ans++;
else if (g[i][j] == 'D')res++;
if (g[i][j] == 'U') {
if (ans % 2 == 1)f[i][j] = 'W';
else f[i][j] = 'B';
}
else if (g[i][j] == 'D') {
if (res % 2 == 0)f[i][j] = 'W';
else f[i][j] = 'B';
}
}
}
rep(i,0,m-1) {
int res = 0, ans = 0;
rep(j,0,n-1) {
if (g[j][i] == 'L')ans++;
else if (g[j][i] == 'R')res++;
if (g[j][i] == 'L') {
if (ans % 2 == 1)f[j][i] = 'W';
else f[j][i] = 'B';
}
else if (g[j][i] == 'R') {
if (res % 2 == 0)f[j][i] = 'W';
else f[j][i] = 'B';
}
}
}
rep(i,0,n-1) {
rep(j,0,m-1)cout << f[i][j];
cout << endl;
}
}
signed main(){
cin>>t;
while(t--)solve();
}
后面的下午在补,先睡觉了@_@