当时打比赛的时候卡在D题了,没有看E。现在看来E还是不难的。

将n个数排序后,其实不排序也是可以的,只是排序能快一半的时间。

枚举前一半能得到多少种和,放到map里面;

然后在后一半数中枚举,然后在map里面查找。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int maxn = ;
LL a[maxn], f[maxn], S, ans;
int n, m, k; map<int, LL> cnt[maxn]; void dfs(int d, int used, LL s, int type)
{
if(d == m)
{
if(type == ) cnt[used][s]++;
else for(int i = ; i + used <= k; i++) if(cnt[i].count(S - s)) ans += cnt[i][S - s];
return;
}
dfs(d + , used, s, type);
if(s + a[d] <= S) dfs(d + , used, s + a[d], type);
if(a[d] < && used < k && s + f[a[d]] <= S) dfs(d + , used + , s + f[a[d]], type);
} int main()
{
f[] = f[] = ;
for(int i = ; i < ; i++) f[i] = f[i - ] * i;
cin >> n >> k >> S;
for(int i = ; i < n; i++) cin >> a[i];
sort(a, a + n);
m = n / ;
dfs(, , , );
m = n;
dfs(n / , , , );
cout << ans << endl; return ;
}

代码君

CodeForces Round #297 Div.2 E (中途相遇法)的更多相关文章

  1. Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索

    Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx  ...

  2. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  3. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  4. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  5. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  6. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  7. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  8. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  9. 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

    题目传送门 /* 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 题目倒是很长:) */ #include <cstdio> #include <algorithm> ...

随机推荐

  1. Java正则表达式匹配例子

    Java正则表达式匹配例子 package com.ibm.test; import java.util.regex.Matcher; import java.util.regex.Pattern; ...

  2. sql连接字符串的方法

    ----乌龟代码---合并列值 --********************************************************************************** ...

  3. 8 simple things that will make you sexy

    8 simple things that will make you sexy8种方法教你不动声色的性感What makes a women sexy? Is it her body? Is it t ...

  4. jQuery年月日(生日)选择器

    我们在编辑用户资料时经常会遇到选择生日选项的问题,今天我给大家介绍如何使用js来实现年月日(生日)选择器,能够准确计算闰年的年月日,方便表单处理. 文章:http://www.helloweba.co ...

  5. iOS开发--邮箱,电话号码,身份证正则表达式验证

    //邮箱 + (BOOL) validateEmail:(NSString *)email {     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@ ...

  6. getElementByClassName封装函数用法

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. android+apimonitor+genymotion

    1. 安装genymotion: http://www.genymotion.net/ 2. 设置使用adb Setting--adb--选择sdk的目录 3. apimonitor https:// ...

  8. Ubuntu刷新DNS

    linux刷新dns的缓存方法是: sudo /etc/init.d/nscd restart 如果发现提示命令找不到: sudo: /etc/init.d/nscd: command not fou ...

  9. HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)

    Seam Carving DescriptionFish likes to take photo with his friends. Several days ago, he found that s ...

  10. 【java】String类和StringBuffer类常用操作

    String类是字符串常量,是不可更改的常量.而StringBuffer是字符串变量,它的对象是可以扩充和修改的.StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于Stri ...