Codeforces Round 862 (Div. 2)

Problem - A - Codeforces

AC代码:

#include
#define endl '\n'
//#define int long long
using namespace std;
const int N=1e3+10;
int a[N];
int n;
void solve() {
	cin>>n;
	int ans=0;
	for(int i=1;i<=n;i++) cin>>a[i],ans^=a[i];
	if(n%2==1){
		for(int i=0;i<(1<<8);i++){
			if((ans^i)==0){
				cout<>t;
	while(t--) {
		solve();
	}
	return 0;
}

Problem - B - Codeforces 

长度为n的字符串
操作有且只有一次,刚好一次
操作:选择一个字符,将它放在开头

输出字典序最小的字符串

先找到最小的字符是哪一个,然后从后往前遍历,找到第一个和最小字符相同的,将它放在开头

AC代码:

#include
#define endl '\n'
//#define int long long
using namespace std;
int n;
string s;
void solve() {
	cin>>n;
	cin>>s;
	char minn='z';
	for(int i=0;i<(int)s.size();i++){
		if(minn>s[i]) minn=s[i];
	}
	int idx=-1;
	for(int i=(int)s.size()-1;i>=0;i--){
		if(s[i]==minn){
			idx=i;
			break;
		}
	}
	cout<>t;
	while(t--) {
		solve();
	}
	return 0;
}

Problem - C - Codeforces

Codeforces Round 862 (Div. 2)_第1张图片

 数形结合,从图中可以看出,找最接近切线的两条线,判断是否和抛物线有交点即可(用求根公式)

注意:当取pos-1时,pos的范围应在1到s.size()

 AC代码:

#include 
#define endl '\n'
#define int long long
using namespace std;
int n, m;
int k;
void solve() {
	cin >> n >> m;
	vectorans;
	while (n--) {
		cin >> k;
		ans.push_back(k);
	}
	sort(ans.begin(), ans.end());
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		double k1 = b + 2 * sqrt(a * c);
		double k2 = b - 2 * sqrt(a * c);
		int pos = lower_bound(ans.begin(), ans.end(), k1) - ans.begin();
		int pos1 = lower_bound(ans.begin(), ans.end(), k2) - ans.begin();
		if (pos>=0&&pos<(int)ans.size()&&(b - ans[pos]) * (b - ans[pos])-4*a*c< 0) {
			cout << "Yes" << endl;
			cout << ans[pos] << endl;
		} else if (pos>0&&pos<(int)ans.size()+1&& (b - ans[pos - 1]) * (b - ans[pos - 1]) - 4 * a * c < 0) {
			cout << "Yes" << endl;
			cout << ans[pos - 1] << endl;
		}
		else if (pos1>=0&&pos1<(int)ans.size()&&(b - ans[pos1]) * (b - ans[pos1])-4*a*c < 0) {
			cout << "Yes" << endl;
			cout << ans[pos1] << endl;
		} else if (pos1>0&&pos1<(int)ans.size()+1&& (b - ans[pos1 - 1]) * (b - ans[pos1 - 1]) - 4 * a * c < 0) {
			cout << "Yes" << endl;
			cout << ans[pos1 - 1] << endl;
		}else{
			cout<<"No"<>t;
	while (t--) {
		solve();
	}
	return 0;
}

你可能感兴趣的:(codeforces,c++,算法)