首页 > 其他分享 >atcoder abc 276

atcoder abc 276

时间:2022-11-10 20:34:42浏览次数:52  
标签:atcoder abc 题意 int cin vis vector 276 ans

A - Rightmost

题意:

给定一个字符串,确定字母a,最后出现的位置,若字符串中没有出现字母a,则输出-1

思路:

遍历统计

代码:

#include <bits/stdc++.h>
using namespace std;



int main() {
	string s;
	cin >> s;
	
	int ans = -1;
	
	for (int i = 0; i <= s.size(); i ++ ) {
		if(s[i] == 'a') {
			ans = i;	
		}
	}
	
	if(ans == -1) cout << -1 << endl;
	else {
		cout << ans + 1 << endl;
	}
	
	return 0;
}

B - Adjacency List

题意:

在一个n个点,m条边的图中,打印每个点直接相连的点(升序)

思路:

用vector存储每个边的直接相邻点

时间复杂度:O(m)

代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;

vector<int> v[N];

int main() {
	int n, m;
	cin >> n >> m;
	
	for (int i = 1; i <= m; i ++ ) {
		int a, b;
		scanf("%d%d", &a, &b);
		
		v[a].push_back(b);
		v[b].push_back(a);
	}
	
	for (int i = 1; i <= n; i ++ ) {
		cout << v[i].size() << " ";
		
		sort(v[i].begin(), v[i].end());
		
		for (int j = 0; j < v[i].size(); j ++ ) {
			cout << v[i][j] << " ";
		}
		
		cout << "\n";
	}
	
	
	
	
	return 0;
}

C - Previous Permutation

题意:

按照规定给予一个序列,打印按照字典序从小到大排序该序列的上一个序列

思路:prev_permutation直接过,(手动模拟的思路有些问题,只能过一半的数据)

时间复杂度:prev_permutation和next_permutation的时间复杂度是O(n)

代码;

#include <bits/stdc++.h>
using namespace std;

const int N = 200;
int a[N];
bool vis[N];


int main() {
	int n;
	cin >> n;
	
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	
	prev_permutation(a + 1, a + 1 + n);
	
	for (int i = 1; i <= n; i ++ ) {
		cout << a[i];
		
		if(i == n) cout << endl;
		else cout << " ";
	}	
	
	
	
	return 0;
}

Divide by 2 or 3

题意:

给定一个数组,对于数组的每一个元素你这可以执行两种操作

1、若a[i] % 2 == 0,你可以用 a[i] / 2 代替 a[i]
2、若a[i] % 3 == 0,你可以用 a[i] / 3 代替 a[i]

若是使得数组中所以元素都相同,你所需的最小操作数是多少,若不行则输出-1

思路:

如果最后能一样,说明a1~an中都可以化为 a1 = b1 * x...。因此a1an中肯定有一个最小的公倍数并且是公共的。而且如果最后能化成一样,说明b1bn都是2的倍数或者3的倍数否则无法除到x。

时间复杂度:O(n)

代码:

#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b) {
	if(b == 0) {
		return a;
	}
	
	return gcd(b, a % b);
}

int main() {
	int n;
	cin >> n;
	
	vector<int> a(n);
	bool flag = false;
	
	for (int i = 0; i < n; i ++ ) {
		cin >> a[i];
	}
	
	int ans = 0;
	int temp = 0;
	
	for (int i = 0; i < n; i ++ ) {
		temp = gcd(temp, a[i]);
	}
	
	for (auto &x : a) {
		x /= temp;
		
		for (; x % 2 == 0; x /= 2) {
			ans ++ ;
		}
		
		for (; x % 3 == 0; x /= 3) {
			ans ++ ;
		}
		if(x != 1) flag = true; 
	}
	
	if(flag) {
		cout << -1 << endl;
	}
	else {
		cout << ans << endl;
	}
	
	
	
	
	return 0;
}

E - Round Trip

题意:

在一个H * W的矩阵中,有障碍物、路和起点,问是否存在一个简单路径重新回到起点,并且路径的长度要大于等于4

思路:

由于数据给定比较特殊所以使用vector来存储,保证图的完整,然后从起点dfs,注意这里的需要排除一些特殊情况,

当离开起点1个单位的时候,若是下一个点在沿着这个方向走回来,那路径就是2,所以需要排除这种情况

时间复杂度:O(H * M)

代码:

#include <bits/stdc++.h>
using namespace std;

#define inf 1e9
const int N = 1e6 + 5;

vector<char> a[N];
vector<int> dis[N];
vector<int> vis[N];
int n, m;
int xx, yy;
int ans;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

void dfs(int x, int y, int s) {
	//找到了 
	if(x == xx && y == yy && s >= 4) {
		ans = 1;
		return ;
	}
	
	if(s <= dis[x][y]) return ;
	dis[x][y] = s;
	
	for (int k = 0; k < 4; k ++ ) {
		int i = x + dx[k];
		int j = y + dy[k];
		
		if(i < 1 || i > n || j < 1 || j > m) continue;
		
		if(a[i][j] == '#' || vis[i][j]) continue;
		
		if((!(i == xx && j == yy)) || s + 1 >= 4) {
			vis[i][j] = 1;
			dfs(i, j, s + 1);
			vis[i][j] = 0;
		}
	
		if(ans) return ;
	}
}

int main() {
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++ ) {
		a[i].push_back(' ');
		vis[i].push_back(0);
		dis[i].push_back(0);
		for (int j = 1; j <= m; j ++ ) {
			char str;
			cin >> str;
			a[i].push_back(str);
			vis[i].push_back(0);
			dis[i].push_back(-inf);
			
			if(str == 'S') {
				xx = i, yy = j;
				dis[i][j] = -1;
			}
		}
	}
		
 	dfs(xx, yy, 0);
	
	if(ans) cout << "Yes" << endl;
	else cout << "No" << endl;
	
	
	return 0;
} 

标签:atcoder,abc,题意,int,cin,vis,vector,276,ans
From: https://www.cnblogs.com/luoyefenglin/p/16878649.html

相关文章

  • ABC246Ex
    DDP板子。设\(f_{i,0/1}\)表示前\(i\)位,以\(0/1\)结尾的本质不同子序列有多少种。则最终答案就是\(f_{n,0}+f_{n,1}\)。考虑转移,以当前字符为0为例,则有\[f_{......
  • Atcoder Grand Contest 004(A~F)
    这场半VP做的,就不分赛时赛后写了,直接放每道题的解法。A-DivideaCuboid当某一维的长度为偶数的时候,显然可以在这一维的中间切,两部分方块的最小差为\(0\)。当每一......
  • ABC 270 D - Stones(博弈DP)
    https://atcoder.jp/contests/abc270/tasks/abc270_d题目大意:给定我们总共n个石子,我们每次拿的数量都必须是数组a中的一个,高桥先手,青木后手。问我们高桥可以拿到的最......
  • ABC242F
    套路的二项式反演。题目要求实际就是两种颜色的棋子所占的行和列都不能有交。设\(f(i,j,k)\)表示在\(i\)行\(j\)列中放\(k\)个棋子使得每行,每列都不为空的方案数......
  • ABC241G
    假设当前在确定玩家\(p\)是否能成为唯一的赢家。假设\(p\)能赢下所有不确定的比赛,令\(win\)表示他赢的数量。如果\(win=0\)显然他不能成为唯一的赢家,下面都假设......
  • ABC231G
    先来研究没有初始球情况下的简单版本:\(n\)个小球,\(m\)个盒子,每个小球等概率地放到盒子里,这样有\(n^m\)种方案,每种方案的贡献是每个盒中球个数的乘积,计算所有方案贡献......
  • R语言近似贝叶斯计算MCMC(ABC-MCMC)轨迹图和边缘图可视化
    近似​​贝叶斯​​计算和近似技术基于随机模拟模型中的样本计算近似似然值,在过​​去几年中引起了很多关注,因为它们有望为任何随机过程提供通用统计技术。一位同事向我询问......
  • ABC235G
    首先有一个\(\mathcalO(N^2)\)做法。考虑容斥掉条件一,令\(g(i)\)表示恰好有\(i\)个花园空着,\(f(i)\)表示至少有\(i\)个花园空着。则有\(g(0)=\sum\limits_{i=......
  • ABC267G
    考虑重新刻画一个序列的生成,设原数列为\((0,0)\),将所有数从小到大排序后依次加入。例如\((2,3,1)\)是这样生成的:\[(0,0)\to(0,1,0)\to(0,2,1,0)\to(0,2,3,1,0)\]于是......
  • 题解 ABC154F【Many Many Paths】
    problem令\(f(i,j)\)表示,在平面直角坐标系中,从\((0,0)\)出发,每次向上或向右走一步,到达\((i,j)\)的方案数,求:\[\sum_{l_1\leqi\leqr_1}\sum_{l_2\leqj\leqr_2}f(......