本文主要是介绍第九届北京化工大学程序设计竞赛(部分题解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
A Math game
Problem Description
Input
In each case, there are two numbers in the first line n (the size of the set) and H. The second line has n numbers {a[1],a[2],......,a[n]}. 0<n<=40, 0<=H<10^9, 0<=a[i]<10^9,All the numbers are integers.
Output
Sample Input
10 87 2 3 4 5 7 9 10 11 12 13 10 38 2 3 4 5 7 9 10 11 12 13
Sample Output
No Yes
Source
Manager
J http://acdream.info/problem?pid=1735#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(ll a,ll b)
{
return a>b;
}
ll sum;
ll a[50];
int mark;
int n,m;
ll t;
void dfs(int cur,ll sum){
if(sum==m){mark=1;printf("Yes\n");return;}
for(int i=cur;i<=n;i++){
if(mark)break;
t=sum+a[i];
if(t<=m)
dfs(i+1,sum+a[i]);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF){
sum=0;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]),sum+=a[i];
if(m>sum)printf("No\n");
else if(m==sum||m==0)printf("Yes\n");
else{
sort(a+1,a+n+1,cmp);
mark=0;
dfs(1,0);
if(!mark)printf("No\n");
}
}
return 0;
}
输油管道
Problem Description
平面上有n个油井,现在要建立一条主干线,用来把所有的油井产出的原油都输送出去,主干线是平行于x轴的一条直线,每个油井通过一条支线把原油输送到主干线上,现在给定n个油井在平面上的坐标,那么应该把主干线建在什么地方才能让所有的支干线的总长度最小呢?
Input
Output
Sample Input
2 0 0 10 10
Sample Output
10
Source
Manager
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int x[1000010],y[1000010];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n;
while(cin>>n){
for(int i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
sort(y+1,y+1+n);
int mid=y[n/2+1];
ll sum=0;
for(int i=1;i<=n;i++)
sum+=abs(y[i]-mid);
printf("%I64d\n",sum);
}
return 0;
}
这篇关于第九届北京化工大学程序设计竞赛(部分题解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!