School training competition ( Second )

A. Medium Number

链接 : Problem - 1760A - Codeforces

School training competition ( Second )_第1张图片

 就是求三个数的中位数 : 

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 2e5+10;

inline void solve(){
	int a[3];
	for(int i=0;i<3;i++) cin >> a[i];
	sort(a,a+3);
	cout << a[1] << endl;
}
 
int main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

B. Atilla's Favorite Problem

School training competition ( Second )_第2张图片

 就是求最大字母的长度 (与a的距离)

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 2e5+10;

int n ;
string s;

inline void solve(){
	cin >> n ;
	cin >> s;
	sort(s.begin(),s.end());
	int ans = (int)(s[n-1]-'a'+1);
	cout << ans << endl;
}
 
int main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

  C. Advantage

链接 : Problem - 1760C - Codeforces

School training competition ( Second )_第3张图片

数据范围小的话,随便弄,直接排序之后,求出最大和第二大的值,然后遍历i,求a[i]与除a[i]之外最大值的差距。

 

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 2e5+10;

int n ;

inline void solve(){
	cin >> n;
	vector a(n),b(n);
	for(int i=0;i> b[i];
		a[i] = b[i];
	}
	sort(b.begin(),b.end());
	int ma = b[n-1] , mi = b[n-2];
	for(int i=0;i> _;
    while(_ --) solve();
    return 0;
}

D. Challenging Valleys

链接 : Problem - 1760D - Codeforces

School training competition ( Second )_第4张图片

School training competition ( Second )_第5张图片

 题目大概就是说 给出一个数组,如果该数组有且仅有 一个山谷形状的子数组,就输出yes,否则返回false;

这题直接模拟就可以了

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 2e5+10;

int n;
int a[N],b[N];

inline void solve(){
 	cin >> n;
 	for(int i=0;i> b[i];
 	if(n==1){
 		cout << "YES" << endl;
		 return ;	
	}
	int len = n;
	n = 0;
	a[n++] = b[0];
    //把连续的数去重
    for (int i = 1; i < len; ++i) {
        if (b[i] != b[i - 1])
            a[n++] = b[i];
    }
	int cnt = 0;
	
	if(n==1 || n==2){
		cout << "YES" << endl;
		return ;
	}
	
	if(a[1]>a[0]) cnt ++;
	if(a[n-2] > a[n-1]) cnt ++;
	
	for(int i=1;ia[i] && a[i] < a[i+1]){
			cnt ++;
		}
	}
	if(cnt == 1) cout << "YES" << endl;
	else cout << "NO" << endl;
	return ;
}
 
int main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

E. Binary Inversions

链接 : Problem - 1760E - Codeforces

School training competition ( Second )_第6张图片

思路 : 要求逆序对的数量最大,那么也就只有三种情况,不改 / 将第一个0改为1 / 将最后的1转换为0, 三种情况分情况讨论即可;

 

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 2e5+10;

inline void solve(){
	int n ; cin >> n;
	LL ans = 0 , res = 0;
	vector a(n);
	for(int i=0;i> a[i];
	int pre = 0;
	for(int i=n-1;i>=0;i--){
		if(a[i]==0) pre++;
		else ans += pre; // 预处理每个 1 后面 有 多少个 0 之和  
	}
	
	pre = 0;
	int t = -1;
	for(int i=0;i=0;i--){
		if(a[i]==0) pre++;
		else res += pre;
	}
	ans = max(ans,res);
	
	if(t!=-1) a[t] = 0;
	
	res = 0;
	pre = 0;
	for(int i=n-1;i>=0;i--){
		if(a[i]==1){ // 将最后的 1 转换为 0 
			a[i]=0;
			break;
		}
	}
	
	for(int i=n-1;i>=0;i--){
		if(a[i]==0) pre++;
		else res += pre;
	}
	
	cout << max(res,ans) << endl;
	return ;
}
 
int main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --) solve();
    return 0;
}

F. Quests

链接 : Problem - 1760F - Codeforces

School training competition ( Second )_第7张图片

 

思路 :  二分求 k 值

/**
*  ┏┓   ┏┓
* ┏┛┻━━━┛┻┓
* ┃       ┃
* ┃   ━   ┃ 
*  ████━████
*  ◥██◤ ◥██◤ 
* ┃   ┻   ┃
* ┃       ┃ 
* ┗━┓ Y  ┏━┛
*   ┃ S  ┃ 
*   ┃ S  ┃ 
*   ┃    ┗━━━┓ 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ 
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛
*/
 
#include
using namespace std;

#define int long long
int n,c,d;
int T;
int a[1000050];

inline bool check(int k){
	int now = 1;
	int res = 0;
	for(int i=1;i<=d;++i){
		res+=a[now];
		++now;
		if(now == k+2){
			now = 1;
		}
		if(now == n +1){
			i += k + 1 - n;
			now = 1;
		}
	}
	return res >= c;
}

signed main()
{
	cin >> T ;
	while(T--) {
		cin >> n >> c >> d;
		for(int i=1; i<=n; ++i) cin >> a[i] ;
		int sum = 0;
		sort(a+1,a+n+1);
		reverse(a+1,a+n+1);
		bool  f = 0;
		if(a[1] * d < c){
			puts("Impossible");
			continue;
		}
		if(d <= n){
			sum = 0;
			for(int i=1;i<=d;++i){
				sum+=a[i];
			}
			if(sum >= c){
				f = 1;
			}
		}
		if(f){
			puts("Infinity");
			continue;
		}
		int res = 0;
		int  l =0;
		int  r =1e16+1;
		while(l<=r){
			int mid = l + r >> 1;
			if(check(mid)){
				res = mid;
				l = mid + 1;
			}
			else{
				r = mid - 1;
			}
		}
		if(res == 1e16 + 1){
			puts("Infinity");
			continue;
		}
		cout<

A - Filter

链接 : A - Filter

School training competition ( Second )_第8张图片

直接模拟就行了

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 2e5+10;

inline void solve(){
	int n ; cin >> n;
	for(int i=0;i> x;
		if(x %2 ==0){
			cout << x << " ";
		}
	}
}
 
int main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

B - ASCII Art

链接 : B - ASCII Art

School training competition ( Second )_第9张图片

也是水题,直接模拟输出即可

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 110;

int a[N][N];
char s[N][N];

inline void solve(){
	int n,m ;cin >> n >> m;
	for(int i=0;i> a[i][j];
	for(int i=0;i> _;
    while(_ --) solve();
    return 0;
}

C - Merge Sequences

链接 : C - Merge Sequences

School training competition ( Second )_第10张图片

题意大概是合并两个升序排列的数组,然后求a,b两个数组的元素在新数组中的下标是多少:

这一题直接模拟合并过程即可,在合并的过程中将对应的下标分别添加到ca,cb两个数组中; 

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;

const int N = 1e5+10;
int a[N],b[N];

int n,m;

inline void solve(){
	cin >> n >> m;
	for(int i=1;i<=n;i++) cin >> a[i];
	for(int i=1;i<=m;i++) cin >> b[i];
	// a , b 递增 
	vector ca,cb; 
	int k = 1 ;
	int p1 = 1 , p2 = 1;
	while(p1 <= n || p2 <= m){
		if(p1 == n+1){
			cb.push_back(k++);
			p2++;
		}else if(p2 == m+1){
			ca.push_back(k++);
			p1++;
		}else if(a[p1] < b[p2]){
			ca.push_back(k++);
			p1++;
		}else{
			cb.push_back(k++);
			p2++;
		}
	}
	for(int x : ca) cout << x << " ";
	cout << endl;
	for(int x : cb) cout << x << " ";
	cout << endl;
}
 
int main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

D - Bank

链接 : D - Bank

School training competition ( Second )_第11张图片

思路 : 模拟

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 5e5+10;
	
int n,q;
int t,x;
bool a[N] = {false}; // 标记 是不是已经 go 

inline void solve(){
	cin >> n >> q;
	int cnt = 1 , now = 1;
	while(q--){
		cin >> t;
		if(t==1){
			cnt ++;
		}else if(t==2){
			cin >> x;// 叫到就一定来了 
			a[x] = true;
		}else{
			while(a[now]) ++now;
			cout << now << endl;
		}
	}
}
 
int main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

E - 2xN Grid

链接 : E - 2xN Grid

School training competition ( Second )_第12张图片

School training competition ( Second )_第13张图片

思路 : 也是模拟 , 一一对应即可,和 C 题类似

#include
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'

using namespace std;
typedef long long LL;
const int N = 1e5+10;

LL l,n,m;
LL v1[N],len1[N];//值 和 个数 
LL v2[N],len2[N];

inline void solve(){
	cin >> l >> n >> m;
	for(int i=1;i<=n;i++) cin >> v1[i] >> len1[i];
	for(int i=1;i<=m;i++) cin >> v2[i] >> len2[i];
	LL ans = 0;
	int a = 1 , b = 1;
	while(a <= n && b <= m){
		if(v1[a] == v2[b]) ans += min(len1[a] , len2[b]);
		if(len1[a] < len2[b]){
			len2[b] -= len1[a];
			a++;
		}else{
			len1[a] -= len2[b];
			b++;
		}
	}
	cout << ans << endl;
}
 
int main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

你可能感兴趣的:(算法学习,CF,算法,c++)