1. 程式人生 > >Codeforces Round #479 (Div. 3)題解 977A 977B 977C 977D 977E 977F

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;
}


相關推薦

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 numbe

Codeforces Round #479 (Div. 3)解題報告

ive 操作規則 false iostream 數組 pre equal sort 報告 題目鏈接:  http://codeforces.com/contest/977   A. Wrong Subtraction 題意 給定一個數x,求n次操作輸出。操作規則:10的倍數

Codeforces Round #479 (Div. 3)

HR round lld div 個數 等於 ble else code 977A - Wrong Subtraction http://codeforces.com/contest/977/problem/A 簡單題 題意如果尾數為0 除10 否則-1 for (in

Codeforces Round #529 (Div. 3)題解

ABCF看到以後立馬就會做了,ABC差一點做到都比dreamoon切得快(總罰時比dreamoon少1分鐘),主要是我在寫C的時候multiset不知道為什麼壞掉了。。。改了好長時間,最後換了堆 存起來再reverse輸出的。。。DE自閉了好長時間然後發現是傻逼題 A.給一個abbcccddddeeeee這

Codeforces Round #529 (Div. 3) 題解

用兩個 次方 題意 技術分享 序列 put enc its 劃分 題目總鏈接:https://codeforces.com/contest/1095 A. Repeating Cipher 題意: 給出一個字符串,然後將其“收縮”,不斷地將連續

Codeforces Round #486 (Div. 3)題解

這周趁著有Div3想上上分,過了3題,900多名。本以為這下上1400穩了吧,沒想到最後一看Rating,1399.。。。看來想變成青名還得繼續打嘍(希望別再掉下去)。言歸正傳,我們來看一下這次Div3的題解A題這題確實比較水,由於輸出任意一組就可以,那麼就暴力了,使用use

Codeforces Round #506 (Div. 3) 題解

a+b 因子 交集 技術分享 its def clas pre round Codeforces Round #506 (Div. 3) 題目總鏈接:https://codeforces.com/contest/1029 A. Many Equal Substrings

Codeforces Round #479 (Div. 3) ---- D. Divide by three, multiply by two (DFS)

題意: 給你一個序列,然後利用這個序列中的元素重新組成一個新的序列,其中每相鄰的兩個元素具有兩種關係的一種,例如x,y 滿足 x/3 = y 或 x*2 = y 思路: 爆搜,對於每一個元素

Codeforces Round #535 (Div. 3) 題解

計算 perm () distinct 應該 arr get space 直接 Codeforces Round #535 (Div. 3) 題目總鏈接:https://codeforces.com/contest/1108 太懶了啊~好久之前的我現在才更新,趕緊補上吧

Codeforces Round #479 (Div. 3) ---- B.Two-gram

題意: 找一個出現次數最多的長度為2的子串 AC程式碼: #include<bits/stdc++.h> using namespace std; #define rep(i,s

Codeforces Round #498 (Div. 3) 簡要題解

復雜 2個 problem val 要求 scan ces 結構 fine [比賽鏈接] https://codeforces.com/contest/1006 [題解] Problem A. Adjacent Replacements

題解】codeforces1029A[Codeforces Round #506 (Div. 3)]A.Many Equal Substrings KMP

題目連結 Description You are given a string ttt consisting of nnn lowercase Latin letters and an integer number kkk. Let’s define a sub

題解】codeforces1029B[Codeforces Round #506 (Div. 3)]B.Creating the Contest 貪心

題目連結 Description You are given a problemset consisting of nnn problems. The difficulty of the iii-th problem is aia_iai​. It is gua

題解】codeforces1029D[Codeforces Round #506 (Div. 3)]D.Concatenated Multiples 排序

Description You are given an array aaa, consisting of nnn positive integers. Let’s call a concatenation of numbers xxx and yyy the

題解】codeforces1029E[Codeforces Round #506 (Div. 3)]E.Tree with Small Distances 優先佇列+dfs

題目連結 Description You are given an undirected tree consisting of nnn vertices. An undirected tree is a connected undirected graph wi

Codeforces Round #486 (Div. 3) C. Equal Sums (題解

OutputIf it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "

# Codeforces Round #529(Div.3)個人題解

Codeforces Round #529(Div.3)個人題解 前言: 閒來無事補了前天的cf,想著最近刷題有點點怠惰,就直接一場cf一場cf的刷算了,以後的題解也都會以每場的形式寫出來 A. Repeating Cipher 傳送門 題意:第一個字母寫一次,第二個字母寫兩次,依次遞推,求原字串

Codeforces Round #531 (Div. 3) ABCDE題解

Codeforces Round #531 (Div. 3) 題目總連結:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 題意: 給一個數n,然後要求你把1,2.....n分為兩個集合,使得兩個集合裡面元素的和的差的絕對

Codeforces Round #535(div 3) 簡要題解

out long codeforce 一個數 nice make 修改 get ice Problem A. Two distinct points [題解] 顯然 , 當l1不等於r2時 , (l1 , r2)是一組解 否則 , (l1 ,

Codeforces Round #441 Div. 2題解

-- 開始 name getch string ast clu 之間 n-n   比賽的時候E調了好久...F沒時間寫T T   A:直接走到短的路上來回走就好了 #include<iostream> #include<cstring>