1. 程式人生 > >牛客國慶集訓派對Day1 A思維 C暴力 E貪心 L最短路

牛客國慶集訓派對Day1 A思維 C暴力 E貪心 L最短路

A

Code:

#include <bits/stdc++.h>
using namespace std;
const int AX = 1e6 + 66;
int a[AX] ;
map<int,int>mp; 
int main(){
	int a, b, c, d , e , f ;
	cin >> a >> b >> c >> d >> e >> f ; 
	int res = 0 ;
	res += min( a , e ) ;
	res += min( b , f ) ;
	res += min
( c , d ) ; cout << res << endl; return 0 ; }

C

Code:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
#define ios ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
using namespace std;
LL a , b , c , p1 , p2 ,q1, q2 ;
LL f( LL x , LL y ){
	return p2 *
x * x + p1 * x + q2 * y * y + q1 * y ; } int main(){ ios; cin >> a >> b >> c ; cin >> p1 >> p2 >> q1 >> q2 ; LL x , y ; LL d = __gcd( a , b ) ; if( c % d ){ cout << "Kuon" << endl; }else{ LL res = INF ; for( LL x = -100000 ; x <= 100000 ;
x ++ ){ y = c - a * x; if( y % b ) continue; y /= b ; res = min( res , f(x,y) ); } cout << res << endl; } return 0 ; }

E 思路:排序,遍歷,小於m的就合併就行了。 Code:

#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
using namespace std;
const int AX = 1e6 + 66;
int d[AX];
int main(){
	ios;
	int n , m ;
	cin >> n >> m ; 
	for( int i = 0 ; i < n ; i++ ){
		cin >> d[i] ; 
	}
	sort( d , d + n ) ; 
	int ans = 0 ;
	for( int i = 1 ; i < n ; i++ ){
		if( d[i] - d[i-1] <= m ){
			ans ++ ; 
		}
	}
	cout << n - ans << endl;
	return 0 ; 
}

L

思路:因為直線上任意走不耗費體力,所以直線到直線距離可以轉化成點到點距離。 求出直線到直線距離,圓到圓距離,直線到圓距離,然後從一條直線(一個點)跑最短路到另一條直線。 Code:

#include <bits/stdc++.h>
#define INF 1000000000000.0
#define ios ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
using namespace std;
const int AX = 1e3 + 66;
int n ; 
double A , B , c1 , c2  ; 
struct Node{
	double x , y , r ; 
}a[AX];
double w[AX][AX];

double cal( double x1 , double y1 , double x2 , double y2 ){
	return ( sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) ) ;
}
bool vis[AX];
double dis[AX];
void djistra( int s ){
	n += 2 ;
	for( int i = 1 ; i <= n ; i ++ ) dis[i] = INF;
	dis[s] = 0.0 ; 
	vis[s] = true;
	int pos = s ; 
	for( int i = 1 ; i < n ; i++ ){
		double minus = INF ;
		for( int j = 1 ; j <= n ; j++ ){
			if( !vis[j] && dis[j] < minus ){
				pos = j ; minus = dis[j] ; 
			} 
		}
		vis[pos] = 1 ;
		for( int j = 1; j <= n ; j++ ){
			if( !vis[j] && dis[j] > dis[pos] + w[pos][j] ){
				dis[j] = dis[pos] + w[pos][j] ; 
			}
		}
	}
}

int main(){
	scanf("%d%lf%lf%lf%lf",&n,&A,&B,&c1,&c2);
	for( int i = 1 ; i <= n ; i++ ){
		scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);
	} 
	for( int i = 1 ; i <= n ; i++ ){
		for( int j = i + 1 ; j <= n ; j++ ){
			w[i][j] = w[j][i] = max( 0.0 , cal( a[i].x , a[i].y , a[j].x , a[j].y ) - a[i].r - a[j].r );
		}
	}
	for( int i = 1 ; i <= n ; i++ ){
		w[i][n+1] = w[n+1][i] = max( 0.0 , fabs( A*a[i].x + B*a[i].y + c1 ) / sqrt( A*A + B*B ) - a[i].r ) ; 
		w[i][n+2] = w[n+2][i] = max( 0.0 , fabs( A*a[i].x + B*a[i].y + c2 ) / sqrt( A*A + B*B ) - a[i].r ) ; 
	}
	w[n+1][n+2] = w[n+1][n+2] = fabs( c1 - c2 ) / sqrt( A*A + B*B );
	djistra( n + 1 ) ;
	printf("%.6lf\n",dis[n]);
	return 0 ; 
}