1. 程式人生 > >Educational Codeforces Round 47 (Rated for Div. 2) A模擬 B思維 C數學 D數學,暴力

Educational Codeforces Round 47 (Rated for Div. 2) A模擬 B思維 C數學 D數學,暴力

A

Code:

#include <bits/stdc++.h>
using namespace std;
const int AX = 1e3 + 66;
int a[AX];
int c[AX];
int main(){
	int n , m ;
	cin >> n >> m ; 
	for( int i = 0 ; i < n ; i++ ){
		cin >> c[i] ; 
	}
	for( int j = 0 ; j < m ; j++ ){
		cin >> a[j] ;
	}
	int res = 0 ; 
	int
pos = 0 ; for( int i = 0 ; i < m ; i ++ ){ if( a[i] >= c[pos] ){ res ++ ; }else i -- ; pos ++ ; if( pos >= n ) break ; } cout << res << endl; return 0 ; }

B 題意:構造字典序最小的串,原串1,0可交換,1,2可交換位置。 思路:1可以隨意變換位置,所以所有1在第一個2的前面,2後面的0都不能越過2來到前面,順序輸出。 Code:

#include <bits/stdc++.h>
using namespace std; int main(){ string s ; cin >> s ; int len = s.size() ; int one = 0 , zero = 0 ; int f = 0 ; int idx ; for( int i = 0 ; i < len ; i++ ){ if( !f && s[i] == '2' ) { f = 1 ; idx = i ; } if( s[i] == '1' ) one ++ ; if( !f && s[i] == '0' ) zero ++
; } for( int i = 0 ; i < zero ; i++ ){ cout << 0 ; } for( int i = 0 ; i < one ; i++ ){ cout << 1 ; } if( f ){ for( int i = idx ; i < len ; i++ ){ if( s[i] == '1' ) continue ; cout << s[i] ; } } cout << endl; return 0 ; }

C 題意:陣列初始都是0,每個操作選一個i,對於每個j位置的數a[j] + x + d * | i - j | 思路:對於選定的i 有公式 ( ( n - i ) * ( n - i + 1) /2 + ( i -1 ) * i / 2 ) * d , 化簡後d所乘的函式是二次函式,在 ( n+ 1 ) / 2 處取最小值,n處取最大值。 Code:

#include <bits/stdc++.h>
#define LL long long 
using namespace std;
const int AX = 1e5 + 66;

int main(){
	int n , m ;
	scanf("%d%d",&n,&m);
	LL x , d ; 	
	LL sum_x = 0LL ; 
	LL sum_d = 0LL ; 
	while( m-- ){
		scanf("%I64d%I64d",&x,&d);
		sum_x +=  1LL * x * n ; 
		if( d >= 0 ) sum_d += 1LL * d * ( n - 1 ) * n / 2 ;
		else sum_d += 1LL * d * ( n - ( n + 1 ) / 2 ) * ( ( n + 1 ) / 2 );
	}
	double res = (double)( sum_x + sum_d ) / (double)n ;
	printf("%.15lf\n",res); 
	return 0 ;
}


D

思路:尤拉篩可得600左右的互質數對就有1e5個,所以暴力即可。 Code:

#include <bits/stdc++.h>
#define LL long long 
using namespace std;
const int AX = 1e5 + 1 ;
int n , m ;
int u[AX];
int v[AX];
int gcd( int a , int b ){
	return !b ? a : gcd( b , a % b ) ; 
}
int main(){
	scanf("%d%d",&n,&m);
	if( m < n - 1 ){
		printf("Impossible\n");
	}else{
		for( int i = 2 ; i <= n ; i++ ){
			u[m] = i ; v[m--] = i - 1 ;
		}
		for( int i = 3 ; i <= n && m ; i++ ){
			for( int j = i - 2 ; j > 0 && m ; j-- ){
				if( gcd(i,j) == 1 ){
					u[m] = i ; v[m--] = j ; 
				}
			}
		}
		if( m ) printf("Impossible\n");
		else{
			printf("Possible\n");
			for( int i = 1 ; u[i] ; i++ ){
				printf("%d %d\n",u[i],v[i]);
			}	
		}
	}
	return 0 ;
}