Codeforces Round #479 (Div. 3)题解 977A 977B 977C 977D 977E 977F

A. Wrong Subtraction

这两句话决定了题意:(如果一个数n结尾的最后一个数字是1,就减1,是0 就去掉。问k次以后这个数是多少

  • if the last digit of the number is non-zero, she decreases the number by one;
  • if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n ;
	int k;
	cin >> n >>k;
	for(int i=0;i<k;i++){
		if(n%10!=0) n--;
		else	n/=10;
	}
	cout << n << endl;
	return 0;
}


B. Two-gram

题意:找s串中出现次数最多的长度为2的字串。

map搞一下复杂度可以降一个等级,当然这个题n方也可以做

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	string s;
	cin >> s;
	string ss;
	map<string,int> mp;
	vector<string> v;
	for(int i=0;i<n-1;i++){
		ss =  s.substr(i,2);
		v.push_back(ss);
		mp[ss]++;
		ss.clear();
	}
	int w = v.size();
	int mx = 0;
	string sss;
	for(int i=0;i<w;i++){
		if(mp[v[i]]>mx){
			mx = mp[v[i]];
			sss = v[i]; 
		}
	}
	cout << sss <<'\n';
	mp.clear(); 
	return 0;
}


C. Less or Equal

题意:给你一个长度为n的数列和一个非负整数k,问是否存在一个数x,使得序列中小于等于x的元素的个数恰好为k。

(!注意这个题有一个坑点,就是如果k是零的话,数列里面有1,要输出-1,不然要输出1(就是比数列里面任何数都小数)

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

int a[200010];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,k;
	cin >> n >> k; 
	for(int i=1;i<=n;i++)	cin >> a[i];
	sort(a+1,a+1+n);
	if(k!=0 && k!=n){
		if(a[k]==a[k+1])	cout <<"-1\n";
		else cout << a[k]<<'\n'; 
	}else{
		if(k==n)	cout << a[n] <<'\n';
		if(k==0){
			if(a[1]==1)	cout<<"-1\n";
			else cout <<"1\n";
		}
	} 
	return 0;
}


D. Divide by three, multiply by two

题意:给n个数,排序,要求对于相邻两个元素ai,ai+1ai,ai+1,满足aI×2=ai+1aI×2=ai+1ai÷3=ai+1

ai÷3=ai+1保证答案存

直接dfs搜索下有没有解就好了

#include<bits/stdc++.h>
#define ll unsigned long long 
using namespace std;

ll a[105];
int n;
map<ll,int> mp;
vector<ll> v;
void dfs(ll x,int c){
	mp[x]--;
	v.push_back(x);
	if(mp[x*2]>0)	dfs(x*2,c+1);
	if(x%3==0 && mp[x/3]>0)	dfs(x/3,c+1);
	if(c==n){
		for(int i=0;i<n;i++)	cout << v[i] << ' ';
		exit(0);
	}
	v.pop_back();
	mp[x]++;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for(int i=1;i<=n;i++)	cin >> a[i],mp[a[i]]++;
	for(int i=1;i<=n;i++)	dfs(a[i],1);
	return 0;
}

E. Cyclic Components

题意:找到所有的单链环的个数

题解:dfs遍历所有连在一起的点,如果点的度数都是2,则sum加一就好了。

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

const int maxn = 2e5 + 100;
vector<int> lj[maxn];
bool vis[maxn];
int n, m, ans;

bool g = 1;

void dfs(int v){
	vis[v] = 1;
	if(lj[v].size()!=2)	g = 0;
	for(auto u : lj[v]){
		if(vis[u])	continue;
		dfs(u);
	}
}

int main()
{
	cin >> n >> m ;
	for(int i=0;i<m;i++){
		int a,b;
		cin >> a >> b;
		a--,b--;
		lj[a].push_back(b);
		lj[b].push_back(a);
	}
	for(int i=0;i<n;i++){
		if(!vis[i]){
			g = 1;
			dfs(i);
			ans += g;
		}
	}
	cout << ans << '\n';
}

F. Consecutive Subsequence

(借鉴了一下一位大佬的博客:https://blog.csdn.net/qq_38185591/article/details/80256487)

题意:给出一个n和n个数的序列,然后让你找到满足两个数之间相差为1的存在的最长的序列,让你输出他们的下标。

大致思路:

跟最长递增子序列差不多,但是他的数据有给的还是挺大的,两种方法,离散化一下,或者map搞一下,map比较好写,所以直接map存一下。接着你只需要保存最长的序列最后一个数的位置和数就可以,那么怎么保存呢?

因为我们需要顺序的从这个序列里找,可以每次把进来的数标记由前面一个数 + 1得到,即可以得到到这个数为止的最长序列的长度。

比如 3 3 4 7 5 6 8 

3 的时候 我们就以m【2】 + 1 得到这个m【3】,但是因为m【2】没出现过,所以直接就是1,然后下一个3也是如此,4的时候就由m【3】 + 1 ,所以m【4】 = 2,然后依次到8,就可以在m【8】的位置保存到从0到8中,最长的序列长度。

边赋值,边找到最大值的下标和数,然后找到以后,有个最大值,用最大值减去长度 + 1是不是就可以得到他的起始数字了,然后再遍历一遍序列,找每个数就可以了,输出下标。


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

const int maxn = 2e5+10;

int a[maxn];
map<int,int> mp;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	int len = -1,ans = -1,id;
	for(int i=0;i<n;i++){
		cin >> a[i];
		int x = a[i];
		mp[x] = mp [x-1] + 1;
		if(mp[x] > len)	 len = mp[x],ans = x;
	}
	cout << len << '\n';
	int r = ans, l = ans - len + 1;
	for(int i=0;i<n;i++){
		if(a[i]==l)	cout << i+1<<" ",l++;
	}
	cout <<'\n';
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值