(AtCoder Beginner Contest 328)A-F

A - Not Too Hard

#include 

using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 3> p3;
int mod = 1e9+7;
// const int maxv = 4e6 + 5;
// #define endl "\n"


void solve()
{
	int n,x;
	cin>>n>>x;
	int ans=0;
	for(int i=0;i<n;i++){
		int c;
		cin>>c;
		if(c<=x) ans+=c;
	}
	cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
   	system("pause");
    return 0;
}

B - 11/11

#include 

using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 3> p3;
int mod = 1e9+7;
// const int maxv = 4e6 + 5;
// #define endl "\n"


void solve()
{
	int n;
	cin>>n;
	int ans=0;
	vector<int> d(n+5);
	for(int i=1;i<=n;i++){
		cin>>d[i];
	}
	for(int i=1;i<=n;i++){
		map<int,int> mp;
		int c=i;
		while(c){
			mp[c%10]++;
			c/=10;
		}
		if(mp.size()==1){
			int tar=mp.begin()->first;
			int res=tar;
			while(res<=d[i]){
				ans++;
				res=res*10+tar;
			}
		}
	}
	cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
   	system("pause");
    return 0;
}

C - Consecutive

前缀和乱搞一下

#include 

using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 3> p3;
int mod = 1e9+7;
// const int maxv = 4e6 + 5;
// #define endl "\n"


void solve()
{
	int n,q;
	cin>>n>>q;
	string s;
	cin>>s;
	s=" "+s;
	vector<int> sum(s.size()+5);
	vector<int> f(s.size()+5);
	for(int i=1;i<n;i++){
		if(s[i]==s[i+1]){
			f[i]=1;
			sum[i]=sum[i-1]+1;
		}
		else sum[i]=sum[i-1];
	}
	sum[n]=sum[n-1];
	while(q--){
		int l,r;
		cin>>l>>r;
		int res=sum[r]-sum[l-1];
		if(f[r]) res--;
		cout<<res<<endl;
	}
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
   	system("pause");
    return 0;
}

D - Take ABC

经典括号匹配,栈板子题

#include 

using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 3> p3;
int mod = 1e9+7;
// const int maxv = 4e6 + 5;
// #define endl "\n"


void solve()
{
	string s;
	cin>>s;
	vector<char> c;
	for(int i=0;i<s.size();i++){
		c.push_back(s[i]);
		while(c.size()>=3&&c[c.size()-1]=='C'&&c[c.size()-2]=='B'&&c[c.size()-3]=='A'){
			c.pop_back();
			c.pop_back();
			c.pop_back();
		}
	}
	for(auto x: c) cout<<x;
	cout<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
   	system("pause");
    return 0;
}

E - Modulo MST

看到题目数据范围,考虑搜索,暴力枚举每个点所连的边,计算选择n个点后的最小代价。
也可以写成状压的形式,即用一个十进制数去表示当前点选/不选的状态。

#include 

using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 3> p3;
int mod = 1e9+7;
// const int maxv = 4e6 + 5;
// #define endl "\n"

ll n,m,k;

vector<pll> e[15];

void add(int u,int v,ll w)
{
	e[u].push_back({v,w});
	e[v].push_back({u,w});
}


ll res=1e18;
bool st[10];


void dfs(ll sum){
	int f=0;
	for(int i=1;i<=n;i++){
		if(st[i]) continue;
		else{
			f=1;
			break;
		}
	}
	if(!f){
		res=min(res,sum%k);
		return ;
	}
	for(int i=1;i<=n;i++){
		if(st[i]==0)continue;
		for(auto &[v,w]:e[i]){
			if(st[v]==1)continue;
			st[v]=1;
			dfs(sum+w);
			st[v]=0;
		}
	}
}


void solve()
{
	cin>>n>>m>>k;
	for(int i=1;i<=m;i++){
		ll u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	//for(int i=1;i<=n;i++) p[i]=i;
	st[1]=1;
	dfs(0);
	cout<<res<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		solve();
	}
   	system("pause");
    return 0;
}

F - Good Set Query

思路:带权并查集板子题,赛时没有任何看的欲望。

#include 

using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef array<ll, 3> p3;
int mod = 1e9+7;
const int maxv = 4e6 + 5;
#define endl "\n"

ll d[N],p[N];


int find(int x)
{
    if(p[x]!=x){
        int tmp=p[x];
        p[x]=find(p[x]);
        d[x]+=d[tmp];
    }
    return p[x];
}

void solve()
{
    int n,q;
    cin>>n>>q;
    set<int> s;
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=q;i++){
        ll a,b,c;
        cin>>a>>b>>c;
        int fa=find(a),fb=find(b);
        if(fa!=fb){
            p[fa]=fb;
            d[fa]=c+d[b]-d[a];
            s.insert(i);
        }
        else{
            if(d[a]-d[b]==c) s.insert(i);
        }
    }
    for(auto x : s) cout<<x<<" ";
    cout<<endl;

}






int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    system("pause");
    return 0;
}

你可能感兴趣的:(算法,c++,数据结构)