您的位置:首页 > 编程语言 > C语言/C++

2015新生赛题目

2015-12-22 13:26 579 查看


1560: 座位安排三

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 66  Solved: 13

[Submit][Status][Web
Board]


Description

 实验室有n个座位,原本编号为1 ~ n。现在Zero将所有座位重新编号。Zero想知道对于给定的数字p,是否存在某个座位的新编号为p,或者某些座位的新编号和为p。


Input

 输入有多组数据,且不多于15组。每组数据第一行有两个整数n(1 <= n <= 100),p(1 <= p <= 100000),表示座位数和要查询的数。接下来又n行,每一行有两个整数a,k(1 <= k <= 10000)。表示原来编号为a座位,现在为k。题目保证不存在相同的a 和相同的k。


Output

 对于每组数据输出两行。如果存在p,则输出“YES”,占一行。下一行输出一个数,为满足要求的座位原编号和。如果存在多种情况则输出原编号和最大的。否则输出“NO”,下一行输出-1。


Sample Input

6 4

1 6

2 5

3 4

4 3

5 2

6 1

6 100

2 5

6 4

3 10

1 15

4 8

5 12


Sample Output

YES

10

NO

-1


HINT

 对第一个样例有 :

原编号为4、6的座位的新编号相加和为4。

原编号为3的座位的新编号相加和为4。

原编号和最大的为10。

思路:
这题就是的0-1背包入门题,不会的看看这里,点我

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
#define T 1000005
#define inf 0x3f3f3f3fL
typedef long long ll;
const int p = 100005;
int a[p];
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif

int n,m,i,j,k,v,w;
while(~scanf("%d%d",&n,&m))
{
memset(a,-inf,sizeof(a));
a[0] = 0;
for(i=0;i<n;++i){
scanf("%d%d",&v,&w);//v原编号,w新编号
if(m>=w){
for(j=m;j>=w;--j){
if(a[j]<a[j-w]+v){//如果j-w的值加上w的值大于j的值,更新
a[j] = a[j-w]+v;
}
}
}
}
if(a[m]<0){
printf("NO\n-1\n");
}
else {
printf("YES\n%d\n",a[m]);
}
}
return 0;
}



1557: 座位安排二

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 248  Solved: 34

[Submit][Status][Web
Board]


Description

 在安排完座位后,富有探(xian)索(zhe)精(dan)神(teng)的Zero想到了这样一问题。如果实验室有n个座位,编号为1 ~ n,要求一个座位安排了人,则其相邻编号的座位不能有人。现有n个座位,m个人,Zero想知道符合上述坐法的方案有多少种。只需考虑座位的不同,无需考虑人的不同,而且所有人都要被安排到。


Input

 输入有多组数据。每组数据占一行,有两个整数n(1 <= n <= 60)和m(1 <= m <= n),空格隔开。n表示实验室的位子个数,m表示人数。


Output

 对于每组数据输出一行表示安排方案的个数。


Sample Input

1 1

2 1

3 1

4 2

5 2

6 3


Sample Output

1

2

3

3

6

4

思路:

这题其实就是高中时的组合数学,这里要用到插空法来做。例如:

6 3

O O O O O O

O表示上面有六个空位,其中有三个人,而且都要有一个人相隔,所以剩下有三个人就有

| O | O | O |

竖线表示能插入的空位。所以就是C(4,3)了。

还有要注意的是用递归求组合会超时,要用高精度来求。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
#define T 65
#define inf 0x3f3f3f3fL
typedef long long ll;

ll comb(ll a,ll b)
{
double cm = 1.0;
while(b>0){
cm *= (double)(a--)/(double)(b--);
}
return cm+0.5;
}

int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif

ll x,y;
while(~scanf("%lld%lld",&x,&y))
{
if(x-y+1<y)printf("0\n");
else
printf("%lld\n",comb(x-y+1,y));
}

return 0;
}


未完待续。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟 算法 c++