1. 程式人生 > >AtCoder Beginner Contest 112題解

AtCoder Beginner Contest 112題解

AtCoder Beginner Contest 112

比賽連結

前言

這場ABC我用了一個小時AK (雖然我晚到了5分鐘)(雖然這是我第一次AK)

所以我寫下這篇部落格作為紀念。

A.Programming Education

題外話

這個名字起得真好:程式設計教育。但其實是道水題。。。

題目大意

給定一個數NN,若N=1N=1則輸出Hello World;若N=2N=2則繼續讀入兩個數A,BA,B,並輸出A+BA+B的結果。

正解程式碼

不用說了,水題,直接粘程式碼。

#include<cstdio>
#include<algorithm>
using namespace std; int main() { #ifdef LOACL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int N; scanf("%d",&N); if(N==1) { puts("Hello World"); } else { int a,b; scanf("%d %d",&a,&b); printf("%d\n",a+b); } return 0; }

B.Time Limit Exceeded

題目大意

給定TTNN組有序點對(ci,ti)(c_i,t_i),要求找到一個點對,使得它的t&lt;=Tt&lt;=Tcc儘可能的小。

思路

emmm…沒什麼可講的,按照題目意思暴力模擬就行了。。。

正解程式碼

#include<cstdio>
#include<algorithm>
using namespace std;

int main() {
	#ifdef LOACL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	int
N,T; int ans=1e9; scanf("%d %d",&N,&T); for(int i=1;i<=N;i++) { int cos,t; scanf("%d %d",&cos,&t); if(t<=T)ans=min(ans,cos); } if(ans==1e9)puts("TLE"); else printf("%d\n",ans); return 0; }

C - Pyramid

題目大意

有一座金字塔,其中心為(Cx,Cy)(C_x,C_y),高度為HH,它周圍的點(x,y)(x,y)的高度為max(0,HCxxCyy)\max(0,H-|C_x-x|-|C_y-y|)。現給定NN個平面上的點(xi,yi)(x_i,y_i)及它們的高度hih_i,求這個金字塔的HH

思路

由於題目給定0Cx,Cy,xi,yi1000\le C_x,C_y,x_i,y_i\le100,所以直接暴力列舉(Cx,Cy)(C_x,C_y),並與給定資訊比對一下即可。

正解程式碼

#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long ll;
const int Maxn=100;

int N;
int cx,cy;
ll h;
struct Node {
	int X,Y;
	ll H;
	bool operator < (const Node &rhs) const {return H>rhs.H;}
}A[Maxn+5];

inline ll abs(ll a) {
	if(a<0)return -a;
	return a;
}

int main() {
	#ifdef LOACL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
		scanf("%d %d %lld",&A[i].X,&A[i].Y,&A[i].H);
	sort(A+1,A+N+1);
	for(cx=0;cx<=100;cx++) {
		bool flag=false;
		for(cy=0;cy<=100;cy++) {
			ll th=max(0LL,abs(A[1].X-cx)+abs(A[1].Y-cy)+A[1].H);
			if(th<1)break;
			for(int i=2;i<=N;i++)
				if(max(0LL,th-abs(A[i].X-cx)-abs(A[i].Y-cy))!=A[i].H) {
					th=-1;
					break;
				}
			if(th!=-1) {
				h=th;
				flag=true;
				break;
			}
		}
		if(flag)
			break;
	}
	printf("%d %d %lld",cx,cy,h);
	return 0;
}

D - Partition

題目大意

MM拆成NN個數,求這NN個數的最大公因數最大的方案。

思路

我們設MM拆分後的NN個數中最大公約數為kk,第ii個數除以kk的商為kik_i,則不難得出以下關係式:M=kk1+kk2++kkNM=kk_1+kk_2+\cdots+kk_N

整理一下:M=k×i=1NkiM=k\times\sum_{i=1}^{N}{k_i}

則不難看出kkMM的因數。

所以我們只需列舉MM的因數,再判斷一下MM除以該因數再除以NN的結果是否大於0即可(因為不能夠有0出現)。

正解程式碼

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int N,M;

int main() {
	#ifdef LOACL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	scanf("%d %d",&N,&M);
	if(N==1) {
		printf("%d\n",M);
		return 0;
	}
	int ans=1;
	for(int i=2;i*i<=M;i++)
		if(M%i==0) {
			int tmp=M/i;
			if(tmp/N>0)
				ans=max(ans,i);
			if(i/N>0)ans=max(ans,tmp);
		}
	printf("%d\n",ans);
	return 0;
}