A Raise Both Hands:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
void solve()
{
int l, r;
cin>>l>>r;
if(l==1 && r!=1){
cout<<"Yes\n";
}
else if(l!=1 && r==1){
cout<<"No\n";
}
else cout<<"Invalid\n";
}
int main()
{
int T;
// cin>>T;
T=1;
while(T--){
solve();
}
return 0;
}
BBinary Alchemy:
思路: 按题意进行模拟。
定义x为当前为第几个元素,y为要与第几个元素进行组合。
循环n次找到当前可以与第几个元素进行组合。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=110;
int a[N][N];
int n;
void solve()
{
cin>>n;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
cin>>a[i][j];
}
}
int x=1, y=0;
int ans=0;
vector<bool> f(n+1);
for(int i=0; i<n; i++){
for(int j=1; j<=n; j++){
if(!f[j]){
y=j;
f[j]=true;
ans=a[max(x,y)][min(x,y)];
x=ans;
}
}
}
cout<<ans<<"\n";
}
int main()
{
int T;
// cin>>T;
T=1;
while(T--){
solve();
}
return 0;
}
CWord Ladder:
思路:要想使X字符串元素最少,则应更改最少的元素让S==T;
要使X字符串字典序最小,考虑每次更改对S字典序的影响,如果s[i] > t[i], S的字典序会变小,如果s[i] < t[i] S的字典序会变大. 我们应让能够让S字典序变小的操作在前,让S的字典序变大的操作靠后, 同时,让越靠前的元素字典序变小,S的字典序会更小,对于让S字典序变大的操作应优先进行位置靠后的操作,这样会让S;字典序增加的更少.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=110;
int a[N][N];
int n;
void solve()
{
string s, t;
cin>>s>>t;
int n=s.size();
vector<int> f(n+1);
int l=1, r=n;
int cnt=0;
for(int i=0; i<n; i++){
if(s[i]<t[i]) f[r--]=i+1, cnt++;
else if(s[i]>t[i]) f[l++]=i+1, cnt++;
}
cout<<cnt<<"\n";
for(int i=1; i<=n; i++){
if(f[i]!=0){
s[f[i]-1]=t[f[i]-1];
cout<<s<<'\n';
}
}
}
int main()
{
int T;
// cin>>T;
T=1;
while(T--){
solve();
}
return 0;
}
DCross Explosion:
思路: 记录每行每列完好的墙的位置,对于每次询问,找到四个方向最靠近的墙进行破坏.
可以用set的lower_bound 函数 快速找到距离最近的墙.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
void solve()
{
int h, w, q;
cin>>h>>w>>q;
vector<set<int>> r(h+1),c(w+1);
for(int i=0; i<=w+1; i++) r[1].insert(i);
for(int i=0; i<=h+1; i++) c[1].insert(i);
for(int i=2; i<=h; i++) r[i]=r[1];
for(int i=2; i<=w; i++) c[i]=c[1];
int ans=0;
while(q--){
int x, y;
cin>>x>>y;
auto hh=r[x].lower_bound(y);
auto ll=c[y].lower_bound(x);
if(*hh==y && *ll==x){
r[x].erase(y);
c[y].erase(x);
ans++;
}
else{
auto ha=hh;
auto he=--hh;
if(*ha<=w){
r[x].erase(*ha);
c[*ha].erase(x);
ans++;
}
if(*he>=1){
r[x].erase(*he);
c[*he].erase(x);
ans++;
}
auto la=ll;
auto le=--ll;
if(*la<=h){
c[y].erase(*la);
r[*la].erase(y);
ans++;
}
if(*le>=1){
c[y].erase(*le);
r[*le].erase(y);
ans++;
}
}
}
// for(int i=1; i<=h; i++)
// cout<<r[i].size()<<'\n';
cout<<h*w-ans<<'\n';
}
int main()
{
int T;
// cin>>T;
T=1;
while(T--){
solve();
}
return 0;
}
EAvoid K Partition:
思路: dp,
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int M=998244353;
void solve()
{
LL n,k;
cin>>n>>k;
vector<LL> a(n+1);
for(int i=1; i<=n; i++){
cin>>a[i];
a[i]+=a[i-1];
}
vector<LL> f(n+1);
unordered_map<LL,LL> tot;
f[0]=1, tot[0]=1;
LL sum=1;
for(int i=1; i<=n; i++){
f[i]=(sum-tot[a[i]-k]+M)%M;
(sum+=f[i])%=M, (tot[a[i]]+=f[i])%=M;
}
cout<<f[n]<<'\n';
}
int main()
{
int T;
// cin>>T;
T=1;
while(T--){
solve();
}
return 0;
}