poj2718 Smallest Difference(dfs+特判,还可以贪心更快)
https://vjudge.net/problem/POJ-2718
其实不太理解为什么10超时了。。
这题似乎是有贪心优化的方法的,我下面直接暴力了。。
暴力之余要特判两个点:1.超时点就是n=10的时候,直接算一下247
2.WA点就是如果有两个数,且一个为0,那样不算先导零,结果按我的代码也是要特判的。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
#include<set>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
char s[];
int a[], vis[], sum1, sum2, mini, n;
void dfs(int cur)
{
if(cur < n/){
for(int i = ; i < n; i++){
if(cur == &&a[i] == ) continue;//排除先导零
if(!vis[i]){
vis[i] = ;
sum1 = sum1*+a[i];
cur++;
dfs(cur);
sum1 = (sum1-a[i])/;
cur--;
vis[i] = ;
}
}
}
else if(cur == n){
mini = min(abs(sum2-sum1), mini);
}
else{
for(int i = ; i < n; i++){
if(cur == n/&&a[i] == ) continue;//排除先导零
if(!vis[i]){
vis[i] = ;
sum2 = sum2*+a[i];
cur++;
dfs(cur);
sum2 = (sum2-a[i])/;
cur--;
vis[i] = ;
}
}
} }
int main()
{
int t;
scanf("%d", &t);
char c = getchar();
while(t--){
gets(s);
int len = strlen(s);
n=, sum1 = , sum2 = , mini = INF;
for(int i = ; i < len; i++){
a[n++] = s[i++]-'';
}
if(n == ){//不特判就超时,只有这一条超时
printf("247\n");
continue;
}
else if(n == &&(a[]==||a[]==)){//如果只有两位,且有一位为0也要特判
printf("%d\n", max(a[], a[]));
continue;
}
dfs();
printf("%d\n", mini);
}
}
poj2718 Smallest Difference(dfs+特判,还可以贪心更快)的更多相关文章
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- (DFS、全排列)POJ-2718 Smallest Difference
题目地址 简要题意: 给若干组数字,每组数据是递增的在0--9之间的数,且每组数的个数不确定.对于每组数,输出由这些数组成的两个数的差的绝对值最小是多少(每个数出现且只出现一次). 思路分析: 对于n ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
- POJ-2718 Smallest Difference
http://poj.org/problem?id=2718 从一些数里面选择一个子集组成一个数,余下的数组成另外一个数,(数不能以0开头)问两个数的差的绝对值最小是多少! 不管是奇数还是偶数,要想绝 ...
- poj2718 Smallest Difference
思路: 暴力乱搞. 实现: #include <iostream> #include <cstdio> #include <sstream> #include &l ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- 【POJ - 2718】Smallest Difference(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...
- Smallest Difference(POJ 2718)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6740 Accepted: 18 ...
随机推荐
- P3403 跳楼机
题解: 据说是最短路经典题 考虑mod c一意义下 我们会发现mod c相同的话我们一定会用最少步数到达,剩余的都用c转移 由于转移图有环所以我们用spfa来dp(其实也可以理解成最短路) wa了好多 ...
- bzoj4520【CQOI2016】K远点对
题解: kd-tree裸题 对每个点维护最近的k个开个堆维护一下
- pycharm创建python模板文件
1.新建一个项目: 2.右键单击项目名称-->选择新建-->编辑模板文件 3.编辑模板文件保存 4.新建文件测试 至此不再重复添加头部信息了
- nginx proxy_pass指令’/’注意事项
1. proxy_pass配置说明 不带/ location /test/ { proxy_pass http://t6:8300; } 带/ location /test/ { proxy_pass ...
- HTML LIST 输入框自动查询追加框,自动过滤 HTML5
<!DOCTYPE HTML> <html> <body> <form action="/example/html5/demo_form.asp&q ...
- python全栈开发day58-mysql存储过程,权限,索引,慢日志,执行计划,分页优化处理
1.存储过程 delimiter // create procedure insert_data(in rows int) begin DECLARE n INT DEFAULT 1; drop ta ...
- Consumer is not subscribed to any topics or assigned any partitions
版本: scala:2.11.8 spark:2.11 hbase:1.2.0-cdh5.14.0 报错信息: java.lang.IllegalStateException: Consumer is ...
- ES集群
1. ElasticSerach集群安装 修改配置文件elasticserach.yml [elk@localhost config]$ vi elasticsearch.yml # ------- ...
- 剑指offer 1,输入一个字符串,将字符串的空格替换成%20
剑指offer 1,输入一个字符串,将字符串的空格替换成%20 function replaceSpace(str){ return str.replace(/\s/g,"% ...
- TF:Tensorflow结构简单应用,随机生成100个数,利用Tensorflow训练使其逼近已知线性直线的效率和截距—Jason niu
import os os.environ[' import tensorflow as tf import numpy as np x_data = np.random.rand(100).astyp ...