第九届北京化工大学程序设计竞赛(部分题解)

补题中

A Math game

Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 256000/128000KB (Java/Others)
Problem Description
Recently, Losanto find an interesting Math game. The rule is simple: Tell you a number  H, and you can choose some numbers from a set {a[1],a[2],......,a[n]}.If the sum of the number you choose is  H, then you win. Losanto just want to know whether he can win the game.
Input
There are several cases.
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
If Losanto could win the game, output "Yes" in a line. Else output "No" in a line.
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
题目大意:给定n 和H  n为数列中元素的个数, 求在数列中能否找到几个数使他们的和是H
解题思路:暴力枚举 剪枝  PS:看运气 题解给的是分两部分DFS

#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;
}


http://acdream.info/problem?pid=1735

输油管道

Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 262144/131072KB (Java/Others)
Problem Description

平面上有n个油井,现在要建立一条主干线,用来把所有的油井产出的原油都输送出去,主干线是平行于x轴的一条直线,每个油井通过一条支线把原油输送到主干线上,现在给定n个油井在平面上的坐标,那么应该把主干线建在什么地方才能让所有的支干线的总长度最小呢?

Input
首先一个正整数n,接下来n行每行两个整数,代表n个油井在平面上的位置。n和坐标都是小于等于1000000的正整数。
Output
输出总的支干线长度的最小值,每个结果占一行。
Sample Input
2
0 0
10 10
Sample Output
10
Source
第九届北京化工大学程序设计竞赛
Manager
解题思路:找Y的中位数,再求和就行了

#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值