ACM实训第十一天

回顾后三个代码

Aggressive cows(搞定)

#include<iostream> 
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int a[100],n,c;

bool fun(int m){
	int cnt=1,cur=0,next=1;
	while(next<n){
		next++;
		if(a[next]-a[cur]>=m){
			cnt++;
			cur=next;
		}
	}
	if(cnt>=c) return true;
	else return false;
	
}

int main(){
	scanf("%d %d",&n,&c);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	int left=a[0],right=a[n-1];
	int ans=0;
	sort(a,a+n);
	while(left<right){
		int mid=((left+right)+1)/2;
		if(fun(mid)){
			ans=mid;
			left=mid+1;
		}else{
			right=mid-1;
		}
	}
	printf("%d",ans);
	return 0;
}

Brainman(搞定)

#include<iostream>
#include<stdio.h>
using namespace std;
const int n=2000000;
int a[n],b[n];

int main(){
	int m;
	scanf("%d",&m);
	for(int k=1;k<=m;k++){
		int p;
		scanf("%d",&p);
		for(int i=0;i<=p;i++){
			scanf("%d",&a[i]);
		}
		
		int ans=0;
		for(int i=1;i<=p;i++)
			for(int j=i+1;j<=p;j++)
				if(a[i]>a[j])
					ans++;
		printf("Scenario #%d:\n%d\n\n",k,ans);
	}
	return 0;
}

 Tokitsukaze and Good 01-String (hard version)

// 遍历字符串s,步长为2检查相邻字符
        for (int i = 0; i < s.size(); i += 2)
        {
            if (s[i] != s[i+1]) // 如果相邻字符不同
                x++; // x加1,表示需要交换
            else // 如果相邻字符相同
            {
                if (last != s[i]) // 如果这个字符与上一次遇到的不同
                    y++; // y加1,因为至少需要一次交换来调整
                last = s[i]; // 更新last为当前字符
            }
        }

比较难记忆,有很多涉及到c++方面的知识,故选择先放弃,理解思路即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
 
using namespace std;
typedef long long ll;
 
const int N = 2e5 + 10;
int n;
string s;
 
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while(T--)
	{
		cin >> n >> s;
		int x = 0, y = 0;
		char last = ' ';
		for (int i = 0; i < s.size(); i += 2)
		{
			if (s[i] != s[i+1])
				x++;
			else
			{
				if (last != s[i])
					y++;
				last = s[i];
			}
		}
		//cout << x << " " << max(1, y) << endl;
		printf("%d %d\n", x, (y > 1) ? y : 1);
	}
	return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值