Atcoder beginner contest 396(ABCD,待补EF)
Atcoder beginner contest 396(ABCD,补E)
A:模拟即可
#include<bits/stdc++.h>
#define N 1005
#define mod 998244353
using namespace std;
typedef long long ll;
void solve()
{
int n;
cin>>n;
vector<int>a(n+1);
for(int i =1 ;i <= n;i++) cin>>a[i];
for(int i = 1; i <= n-2;i++)
{
if(a[i] == a[i+1] && a[i+1] == a[i+2])
{
cout<<"Yes\n";
return;
}
}
cout<<"No";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
while(T--)
solve();
return 0;
}
B:栈模拟
#include<bits/stdc++.h>
#define N 1005
#define mod 998244353
using namespace std;
typedef long long ll;
void solve()
{
int n;
cin>>n;
stack<int>q;
while(n--)
{
int op;
cin>>op;
if(op == 1)
{
int x;
cin>>x;
q.push(x);
}
else
{
if(!q.empty())
{cout<<q.top()<<"\n";
q.pop();}
else cout<<0<<"\n";
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
while(T--)
solve();
return 0;
}
C:贪心+双指针
题意描述:有 NN 黑球和 MM 白球。
每个球都有一个值。第 ii 个黑球( 1≤i≤N1≤i≤N )的值为 BiB**i ,第 jj 个白球( 1≤j≤M1≤j≤M )的值为 WjW**j 。
选择0个或多个球,使选中的黑球的数量至少等于选中的白球的数量。在所有这些选择中,找出所选球值的最大可能和。
idea:题意中要求黑球能尽量多选(至少选一个白球的情况。不如直接找两个共同的<0的边界,然后判断选黑球和白球的数量,不满足条件且是因为白球导致的<0,则选所有黑球,不满足则白球和黑球各选一个,直到sum黑白<0
- 使用双指针
i和j分别指向黑球和白球的末尾(最大值)。 - 从后向前遍历,选择黑球和白球中值最大的球,直到其中一个数组被遍历完或遇到负值。
- 每次选择一对黑球和白球,将其值加入答案
ans。 - 计算已经选择的黑球数量
cnta和白球数量cntb。 - 如果已经选择的白球数量多于黑球数量,并且剩余的白球值都小于等于 0,则只选择剩余的黑球(因为白球不能再增加总和)。
- 将剩余的黑球中值大于等于 0 的球加入答案。
- 如果剩余的白球和黑球中仍有可以增加总和的组合(即 a[i]+b[j]≥0a[i]+b[j]≥0),则继续选择。
- 否则,停止选择。
#include<bits/stdc++.h>
#define N 1005
#define mod 998244353
using namespace std;
typedef long long ll;
#define int long long
void solve()
{
int n,m;
cin>>n>>m;
vector<int>a(n+1),b(m+1);
for(int i =1 ;i <= n;i++) cin>>a[i];//黑
for(int i =1;i <= m;i++) cin>>b[i];//白
sort(a.begin()+1,a.end());
sort(b.begin()+1,b.end());
int i = n,j = m;
ll ans = 0;
//int cnta = 0,cntb = 0;
while(i >= 1 && j >=1)
{
if(a[i] >=0 && b[j] >= 0)
{
ans +=a[i]+b[j];
}
else break;
i--,j--;
}
int cnta = n-i;
int cntb = m-j;
if(cnta <= cntb && b[j] <= 0)
{
while(i >= 1)
{
if(a[i] >=0) ans+=a[i];
i--;
}
}
else
{
while(i >= 1 && j >=1)
{
if(a[i] + b[j] >= 0)
{
ans +=a[i]+b[j];
}
else break;
i--,j--;
}
}
cout<<ans;
}
signed main()
{
//freopen("data.out","r",stdin);
//freopen("my.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
while(T--)
solve();
return 0;
}
(虽然隐约感觉有点不对劲,但确实ac了,对拍器拍了好多组数据也没啥错)(后面仔细想了想|+ai解释确实是对的)
D:dfs
最近atcoder考最短路好多
导致上来就写了个dij板子发现是异或,没有什么特殊的性质,然后观察到n<=10,遂尝试dfs(赛后2min过也算过(bushi))
#include<bits/stdc++.h>
#define N 105
#define mod 998244353
using namespace std;
#define int long long
typedef long long ll;
vector<pair<int,int>>G[N];
void addedge(int u,int v,int w)
{
G[u].emplace_back(v,w);
}
int n;
bool used[N];
void solve()
{
int m;
cin>>n>>m;
for(int i = 1;i <= m;i++)
{
int u,v,w;
cin>>u>>v>>w;
addedge(u,v,w);
addedge(v,u,w);
}
int ans = LONG_LONG_MAX;
vector<int>num;
queue<int>g;
used[1] = 1;
int k =0;
g.push(1);
auto dfs=[&](auto && dfs,int u,int xo)->void
{
if(u == n)
{
ans = min(ans,xo);
return ;
}
for(auto &edge : G[u])
{
auto [v,w]= edge;
if(!used[v])
{
used[v] =1;
k^=w;
dfs(dfs,v,xo^w);
used[v] = 0;
}
}
};
dfs(dfs,1,0);
cout<<ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
while(T--)
solve();
return 0;
}

浙公网安备 33010602011771号