A.
模拟
#include<bits/stdc++.h>
#define INF (1ll<<60)
using namespace std;
typedef long long ll;
const int N=2e5+9;
int t[N];
inline void lan(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>t[i];
}
int a=1;
int b=1;
int c=0;
for(int i=1;i<=n;i++){
if(t[i]==1){
if(!a){
cout<<"NO"<<'\n';
return;
}
if(b){
b=0;
c=1;
continue;
}
if(c){
c=0;
b=1;
continue;
}
}else if(t[i]==2){
if(!b){
cout<<"NO"<<'\n';
return;
}
if(a){
a=0;
c=1;
continue;
}
if(c){
c=0;
a=1;
continue;
}
}else if(t[i]==3){
if(!c){
cout<<"NO"<<'\n';
return;
}
if(b){
b=0;
a=1;
continue;
}
if(a){
a=0;
b=1;
continue;
}
}
}
cout<<"YES"<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int q=1;
while(q--){
lan();
}
return 0;
}
B.
暴力找满足的,或者直接打表
#include<bits/stdc++.h>
#define INF (1ll<<60)
using namespace std;
typedef long long ll;
const int N=2e5+9;
int t[11]={1,6,28,120,496,2016,8128,32640,130816,523776,1};
inline void lan(){
int n;
cin>>n;
for(int i=9;i>=0;i--){
if(n%t[i]==0){
cout<<t[i]<<'\n';
return;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int q=1;
while(q--){
lan();
}
return 0;
}
C.
并查集,维护有关系的最小传播代价
#include<bits/stdc++.h>
#define INF (1ll<<60)
using namespace std;
typedef long long ll;
const int N=2e5+9;
int c[N],fa[N];
int find(int x){
if(fa[x]==x){
return x;
}else{
return fa[x]=find(fa[x]);
}
}
void merge(int a,int b){
a=find(a);
b=find(b);
if(a!=b){
fa[a]=b;
c[b]=min(c[b],c[a]);//处理出最小值连接这个连通块的最小代价
}
}
inline void lan(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
fa[i]=i;
}
for(int i=1;i<=n;i++){
cin>>c[i];
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
merge(x,y);
}
ll ans=0;
for(int i=1;i<=n;i++){
if(find(i)==i){//把每个连通块形成价值累加即可
ans+=c[i];
}
}
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int q=1;
while(q--){
lan();
}
return 0;
}
D.
通过两种决策来维护余额上下界
#include<bits/stdc++.h>
#define INF (1ll<<60)
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N];
inline void lan(){
int n,d;
cin>>n>>d;
for(int i=1;i<=n;i++){
cin>>a[i];
}
//上界下界,账户余额,两种决策
ll sum1=0;//充最少的钱,但能满足
ll sum2=0;//充最多的钱,但满足
ll ans=0;
for(int i=1;i<=n;i++){
if(a[i]==0){
if(sum1<0){
sum1=0;
}
if(sum2<0){
sum2=d;
ans++;
}
}else{
sum1+=a[i];
sum2+=a[i];
if(sum1>d){//下界都超过了d,因此必然不能满足
cout<<-1<<'\n';
return;
}
if(sum2>d){
sum2=d;
}
}
}
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int q=1;
while(q--){
lan();
}
return 0;
}
文章介绍了使用C++编程语言解决三个不同问题的解决方案:模拟判断满足条件的序列,暴力查找满足整除关系的最小值,以及运用并查集维护有关系的最小传播代价。最后一个是关于维护账户余额上下界的决策问题。
237

被折叠的 条评论
为什么被折叠?



