AtCoder Beginner Contest 112題解
阿新 • • 發佈:2018-12-13
AtCoder Beginner Contest 112
前言
這場ABC我用了一個小時AK (雖然我晚到了5分鐘)(雖然這是我第一次AK)。
所以我寫下這篇部落格作為紀念。
A.Programming Education
題外話
這個名字起得真好:程式設計教育。但其實是道水題。。。
題目大意
給定一個數,若則輸出Hello World
;若則繼續讀入兩個數,並輸出的結果。
正解程式碼
不用說了,水題,直接粘程式碼。
#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
題目大意
給定和組有序點對,要求找到一個點對,使得它的且儘可能的小。
思路
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
題目大意
有一座金字塔,其中心為,高度為,它周圍的點的高度為。現給定個平面上的點及它們的高度,求這個金字塔的。
思路
由於題目給定,所以直接暴力列舉,並與給定資訊比對一下即可。
正解程式碼
#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
題目大意
將拆成個數,求這個數的最大公因數最大的方案。
思路
我們設拆分後的個數中最大公約數為,第個數除以的商為,則不難得出以下關係式:
整理一下:
則不難看出是的因數。
所以我們只需列舉的因數,再判斷一下除以該因數再除以的結果是否大於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;
}