Preparing Olympiad
来源:互联网 发布:剑雨江湖仙器进阶数据 编辑:程序博客网 时间:2024/05/22 22:40
You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.
Find the number of ways to choose a problemset for the contest.
The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 106) — the difficulty of each problem.
Print the number of ways to choose a suitable problemset for the contest.
3 5 6 11 2 3
2
4 40 50 1010 20 30 25
2
5 25 35 1010 10 20 10 20
6
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable.
题意:
就是给出和的最大值与最小值来规定范围,然后要在选的数中找出最大值与最小值相减大于等于x。求出有几种选择的方案。此题有两种解法,一种dfs去遍历,另外一种就是位运算暴力解法。因为从1 - 2<<15的范围内每个数的位数0,1组合是不同的,不会出现重复的现象。所以适合用是1就加数。不懂我在说什么的可以先看看位运算之类的知识先。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<set>using namespace std;typedef __int64 ll;#define T 20int a[T],bo[T],n,rn,ln,xn,cnt;void dfs(int i,int sum){int k;if(sum>rn)return;if(sum>=ln&&sum<=rn){int mi=0x3f3f3f3f,ma=0;for(int j=0;j<n;++j){if(bo[j])mi=min(mi,bo[j]);ma=max(ma,bo[j]);}//一开始少了if结果mi取0了(bo初值为0)if(ma-mi>=xn)cnt++;}for(k=i;k<n;++k){if(!bo[k]){ bo[k]=a[k]; dfs(k+1,sum+a[k]); bo[k]=0;}}}int main(){ /* freopen("input.txt","r",stdin);*/int i;while(~scanf("%d%d%d%d",&n,&ln,&rn,&xn)){for(i=0;i<n;++i){scanf("%d",&a[i]),bo[i]=0;}cnt=0;dfs(0,0);printf("%d\n",cnt);} return 0;}
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <math.h>#include <algorithm>using namespace std;const int inf = 0x3f3f3f3f;int main(){ int n, l, r, x; int a[20]; while(~scanf("%d%d%d%d",&n,&l,&r,&x)) { for(int i = 0; i < n; i ++) { scanf("%d",&a[i]); } int ans = 0; for(int i = 1; i <= (1 << n); i ++) { int cnt = 0; int sum = 0; int maxn = 0; int minn = inf; for(int j = 0; j < n; j ++) { if((1 << j) & i) { cnt ++; sum += a[j]; minn = min(minn, a[j]); maxn = max(maxn, a[j]); } } if(cnt >= 2 && maxn - minn >= x && sum >= l && sum <= r) ans ++; } printf("%d\n", ans); } return 0;}
- Preparing Olympiad
- Preparing Olympiad
- Preparing Olympiad
- Codeforces550B:Preparing Olympiad
- B. Preparing Olympiad
- dfs Preparing Olympiad
- CodeForces 550B Preparing Olympiad
- codeforces-550B-Preparing Olympiad
- codeforces 550B Preparing Olympiad
- Preparing Olympiad CodeForces - 550B
- 【codeforces 550B】Preparing Olympiad
- codeforces 550B Preparing Olympiad(DFS+回溯)
- codeforces 550B Preparing Olympiad(枚举)
- 枚举暴力 cf B. Preparing Olympiad
- codeforces 306 div.2 B. Preparing Olympiad
- cf#306-B. Preparing Olympiad-bitmask暴力
- CodeForces 550B Preparing Olympiad 简单DFS
- CodeForces 550B Preparing Olympiad 暴搜
- 类、对象、包
- HDOJ 4277 USACO ORZ 搜索+剪枝
- 惠普软件试训第一阶段成果展示
- linux下使用usb总线实现类似于tcp/ip的server/client通信
- 使用libnodave采集西门子840D数控系统S7-300 PLC数据
- Preparing Olympiad
- 判断字符串中的字符是否全都不同(字符类算法)
- 用两个栈实现队列
- 编程修养
- 二叉树的多种遍历——Java
- java.lang.UnsupportedClassVersionError: Bad version number in .class file
- Python打开文件时的几种模式比较
- HttpServlet
- 转载自伯乐在线