[算法学习记录] ABC405 A-D
A - Is it rated?
直接判断就行。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void solve()
{
	int r,x;cin >> r >> x;
	
	if(x==1)
	{
		if(r>=1600&&r<=2999) cout << "Yes" <<"\n";
		else cout << "No\n";
	} 
	else 
	{
		if(r>=1200&&r<=2399) cout << "Yes\n";
		else cout << "No\n";
	}
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int _ = 1;
	while(_--)solve();
	return 0;
}
B - Not All
先把1~m存在map中统计一下数量,如果有小于1的,直接标记为\(false\);
接着倒序遍历数组,每次循环\(ans\)加1,如果有\(1~m\)之间的数,递减,接着判断合法性。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 105;
int a[N];
map<int,int>mp;
void solve()
{
	int n,m;cin >> n >> m;
	for(int i = 1;i<=n;i++)cin >>a[i];
	
	for(int i = 1;i<=n;i++)
	{
		if(a[i]<=m)
		{
			if(!mp.count(a[i]))mp[a[i]] = 1;
			else mp[a[i]]++;
		}
	}
	bool judge = true;
	
	for(int i = 1;i<=m;i++)
	{
		if(mp[i]<1) 
		{
			judge = false;
			break;
		}
	}
	
	int ans = 0;
	if(judge)
	{
		for(int i = n;i>=1;i--)
		{
			ans++;
			if(mp.count(a[i]))
			{
				mp[a[i]]--;
				if(mp[a[i]]<1) break;
			}
		}
	}
	else ans = 0;
	
	cout << ans <<"\n";
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int _ = 1;
	while(_--)solve();
	return 0;
}
C - Sum of Product
\(\displaystyle \sum_{1\leq i< j\leq N} A_iA_j = \displaystyle \sum_{1\leq i \leq N,i\leq j} A_iA_j\) ,再利用前缀和优化一下即可。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5+5;
ll a[N],prefix[N]; 
void solve()
{
	int n;cin >> n;
	for(int i = 1;i<=n;i++)cin >> a[i];
	for(int i = 1;i<=n;i++) prefix[i] = prefix[i-1]+a[i];
	
	ll ans = 0;
	
	for(int i = 1;i<=n;i++) ans += a[i]*(prefix[n]-prefix[i]);
	
	cout << ans <<"\n";
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int _ = 1;
	while(_--)solve();
	return 0;
}
D - Escape Route
就是个BFS,赛时没看出来,还以为是Floyd。
不过这次BFS搜索的起点是出口,如果出口向左遍历到'.'那这个点就是">"。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e3+5;
char a[N][N];
int h,w;
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
string ds = "v^><";
queue<pii> q;
bool inmp(int x,int y)
{
	return x>=1&&y>=1&&x<=h&&y<=w;
}
void bfs()
{	
	while(q.size())
	{
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		
		for(int i = 0;i<4;i++)
		{
			int x1 = x+dx[i],y1 = y+dy[i];
			if(inmp(x1,y1)&&a[x1][y1]=='.')
			{
				a[x1][y1] = ds[i];
				q.push({x1,y1});
			}
		}
	}
}
void solve()
{
	cin >> h >> w;
	for(int i = 1;i<=h;i++)
		for(int j = 1;j<=w;j++)
		{
			cin >> a[i][j];
			if(a[i][j]=='E')q.push({i,j});	
		}
		
	bfs();	
		
	for(int i = 1;i<=h;i++)
	{
		for(int j = 1;j<=w;j++)cout << a[i][j];
		cout << "\n";
	}
		
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int _ = 1;
	while(_--)solve();
	return 0;
}
                    
                
                
            
        
浙公网安备 33010602011771号