1. 程式人生 > >codeforce Round 54 Div.2 A,B,C

codeforce Round 54 Div.2 A,B,C

A. Minimizing the String

從前往後遍歷,如果當前字母比後一位字母大,則刪除這個一個(停止遍歷),如果遍歷完,則刪除最後一個。

#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
	int n;
	whib(cin>>n>>s){
		int pre=s.size();
		for(int i=0;i<s.size()-1;++i){
			if(s[i]>s[i+1]){
				s.erase(s.begin()+i);
				break;
			}
		}
		if(s.size()==pre){
			s.erase(s.end()-1);
		}
		cout<<s<<endl;
	}
	return 0;
} 

B. Divisor Subtraction

如果該數是素數那就是1,如果是偶數那/2就行了,如果是奇數,我們應該知道素數除了2以為都是奇數,所以他的最小素數因子肯定是個奇數(是2那就是偶數了,一個奇數減去奇數,那就是是個偶數,/2就行,如果直接用樸素的方法判斷素數還是會T,可以用一個素數柵。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=5000000+5;
ll n;
bool is_prime[MAX+1];//判斷是不是素數
void solve(ll n) {
	fill(is_prime,is_prime+MAX+1,true);
	is_prime[0]=false;
	is_prime[1]=false;
	for(int i=2; i<=n; ++i) {
		if(is_prime[i]) {
			for(int j=i*2; j<=n; j+=i)
				is_prime[j]=false;
		}
	}
}
int main() {
	whib(cin>>n) {
		bool flag=1;
		for(ll i=2;i*i<=n;++i){
			if(n%i==0){
				flag=0;
				break;
			}
		}
		if(flag) {
			cout<<1<<endl;
		} else {
			solve(sqrt(n)+1);
			ll res=0;
			if(n%2==0) { //n為偶數
				cout<<n/2<<endl;
			} else { //n為奇數
				for(ll i=2; i*i<=n; ++i) {
					if(is_prime[i] && n%i==0) {
						n-=i;
						res++;
						break;
					}
				}
				res+=n/2;
				cout<<res<<endl;
			}
		}
	}
	return 0;
}

C. Meme Problem 

第一眼看上去是個二分(二分了很久沒二分出來。。。。最後發現是個二元一次方程

#include<bits/stdc++.h>
using namespace std;
const double cnt=1e-6;
int T;
double d;
int main() {
	scanf("%d",&T);
	while(T--) {
		cin>>d;
		double tt=d*d-4*d;
		if(tt<0) {
			cout<<"N"<<endl;
		} else {
			double ans1=(d+sqrt(tt))/2;
			double ans2=(d-sqrt(tt))/2;
			printf("Y %.9lf %.9lf\n",ans1,ans2);
		}

	}
	return 0;
}

補題flag