ZOJ 2734 Exchange Cards

题目大意:

给定一个值N,以及一堆卡片,每种卡片有一个值value和数量number。求使用任意张卡片组成N的方式。

例如N = 10 ,cards(10,2)(7,2)(5,3)(2,2)(1,5),则10 = 10,10 =7+3+1,10=5+5…

思路分析:

由于之前做过1204,知道这题就是赤裸裸的搜索,直接用dfs暴力即可求得。

可做的优化处理就是——这个过程是贪心Greedy的,先从大到小这样取过来,所以,可以做一步降序排列的预处理。

如上例:我选择了7,那么我就不会去选择10,因为加上10太大了,超出了N。

知识点总结:

1) DFS走法:沿着可行的解空间往前走;

dfs(index, number, sum, target);

2) 循环走法:选择下一个起点枚举;

dfs(++index, 1, sum, target);

代码:

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<fstream>
using namespace std; struct Cards
{
int num;
int value;
bool operator <(const Cards& card)const
{
return value > card.value;
}
}; int totalCardsCount;
vector<Cards> inputValues; void dfs(int index, int number, int sum, int target)
{
if (sum == target) //记录总述
{
totalCardsCount++;
return;
}
if (index == inputValues.size()) return; //长度 //能加则加,deep ing ... ...
if (sum + inputValues[index].value <= target && number <= inputValues[index].num)
{
sum += inputValues[index].value;
number++;
dfs(index, number, sum, target);
number--;
sum -= inputValues[index].value;
} dfs(++index, , sum, target);
} int main()
{
//ifstream cin("2734.txt");
int T, i, target;
int c = ;
int n;
bool first = true;
while (cin >> target){
if (!first)
{
cout << endl;
}
first = false;
c = ;
cin >> n;
inputValues.clear();
int tmp;
for (i = ; i <= n; i++)
{
Cards cd;
cin >> cd.value;
cin >> cd.num;
inputValues.push_back(cd);
}
std::sort(inputValues.begin(), inputValues.end());
totalCardsCount = ;
dfs(, , , target);
cout << totalCardsCount << endl;
}
return ;
}

ZOJ 1947 The Settlers of Catan

题目大意:

给出一个无向图,求这个无权无向图的最长路径——the longest path。即,在图中找一条边不重复(点可以重复)的路径,所得的路径的边的条数即为所求。

如:3个点,两条边,(0,1)(1,2),则the longest path 为2。

图的最大规模为25*25。

思路分析:

看到这题,试图去网上搜索最长路径的知识点,但是始终找不到有用的。最后,我将起点放在欧拉回路的概念上,然后又了解到哈密顿回路,得出一个结论,求最长路径比欧拉回路更为广泛。

不小心浏览到维基百科,知道这是一个NP问题,其算法只能是暴力枚举。更了解到如果是有向图的最长路径的话应该先做拓扑排序预处理。

于是,直接对每个点进行dfs搜索,每次保存最长路径即可。

知识点总结:

1) 欧拉回路:经过图中所有边一次仅一次且行遍所有顶点的回路。

2) 哈密顿回路:经过图中所有顶点一次仅一次的回路。

代码:

#include <stdio.h>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstring>
#include<fstream>
#include<list>
using namespace std; int graph[][];
bool visited[][]; int theLongestPath;
void dfs(int root, int d)
{
if (d > theLongestPath)
{
theLongestPath = d;
} for (int i = ; i < ; i++)
{
if (root == i) continue;
if (graph[root][i] == && visited[root][i] == )
{
visited[root][i] = visited[i][root] = ;
++d;
dfs(i, d);
--d;
visited[root][i] = visited[i][root] = ;
}
}
} int main1947()
{
//fstream cin("1947.txt");
int n, m;
while (cin >> n >> m)
{
if (n == && m == )break;
memset(graph, , sizeof(graph)); int start, end;
for (int i = ; i < m; i++)
{
cin >> start >> end;
graph[start][end] = graph[end][start] = ;
} theLongestPath = ;
for (int i = ; i < n; i++)
{
memset(visited, , sizeof(visited));
dfs(i, );
}
cout << theLongestPath << endl; }
return ;
}

ZOJ 1978 Assistant Required

题目大意:

给定一个队列,2,3,。。。n,每次取队首元素作为幸运元素,然后将其后的每隔i个给拖出去干活。比如,第一次2是幸运的,4,6,8…就要干活;第二次3是幸运的,9,15,21。。。就要去干活。。。

求第K个幸运数字。

思路分析:

从2写道20的序列分析,发现和素数很像,甚至就做成了素数表。

但当测试第20个幸运数时,Sample Out给出的是83,我打出来的是71(还是73,具体忘了),发现错了,于是再分析之下,发现和素数表有点区别。

素数表:每次去除i的倍数;

幸运数字(暂且这么称吧)表:每次去除每隔i的数。

修改之,即可。

知识点总结:

1) 素数表;

代码:

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<fstream>
using namespace std; const int MAX_Prime = ;
const int SEARCH_INT = ; int prime_like[MAX_Prime];
bool is[SEARCH_INT]; void make_prim_table()
{
for (int i = ; i < MAX_Prime; i++)
{
is[i] = ;
}
int num = ;
for (int i = ; i < SEARCH_INT; i++)
{
if (is[i] == ) continue;
if (is[i] == )
{
prime_like[num++] = i;
if (num == MAX_Prime) break;
}
if (i == )
{
for (int k = i; k < SEARCH_INT; k += i)
{
is[k] = ;
}
}
else
{
int number = ;
for (int k = i + ; k < SEARCH_INT; k++)
{
if (is[k] == ) //已经出队
{
if (++number == i)
{
is[k] = ;
number = ;
}
}
}
}
}
} int main1978()
{
make_prim_table();
//fstream cin("1978.txt");
int n;
while (true)
{
cin >> n;
if (n == )break;
cout << prime_like[n] << endl;
}
return ;
}

Acceptted is the best thing for you ~

重拾ZOJ 一周解题的更多相关文章

  1. 重拾《 两周自制脚本语言 》- Eclipse插件实现语法高亮

    源码库: program-in-chinese/stone-editor-eclipse 参考: FAQ How do I write an editor for my own language? D ...

  2. CSS魔法堂:重拾Border之——更广阔的遐想

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  3. CSS魔法堂:重拾Border之——不仅仅是圆角

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  4. CSS魔法堂:重拾Border之——图片作边框

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  5. CSS魔法堂:重拾Border之——解构Border

    前言  当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...

  6. 重拾Blog

    上个月是我入职现在的公司三周年的月份,所以又续订了五年的合同,最近有一些思考,也不知道这个五年能否还会一直在这个公司工作. 一切随缘吧. 闲适有毒,忙碌的时光总是过的很快,自从加入这个公司以来,日常的 ...

  7. [linux]重拾linux

    起因 因为想重拾起linux,同时需要用docker起几个镜像,用来学习网络知识.本来想直接去阿里云上买,后来一想自己机器上,起一个linux是个不错的选择,毕竟不花钱! 还可以用来做本地测试,学习使 ...

  8. 重拾qt

    最近公司又接了一个煤矿的项目,要写个小程序摘取数据,我是公司唯一c++程序员,本来搞ios搞好好的,现在又得重拾半年没摸得qt了.呵呵...呵呵呵. 这里只记录这次小程序的一些小的总结吧.. 1.中文 ...

  9. 重拾linux

    重拾linux 起因 因为想重拾起linux,同时需要用docker起几个镜像,用来学习网络知识.本来想直接去阿里云上买,后来一想自己机器上,起一个linux是个不错的选择,毕竟不花钱! 还可以用来做 ...

随机推荐

  1. poj1013.Counterfeit Dollar(枚举)

    Counterfeit Dollar Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 415  Solved: 237 Description Sally ...

  2. Goldbach's Conjecture

     Goldbach's Conjecture Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  3. 变色龙安装程序 Chameleon Install 2.2 svn 2281发布

    变色龙安装程序 Chameleon Install 2.2 svn 2281发布 1.更好的支持10.9 Mavericks2.更新ATi.nVidia显卡支持列表3.添加新的 CPU Model I ...

  4. C语言register关键字—最快的关键字 ---------------转自http://blog.sina.com.cn/s/blog_6a1837e90101128k.html

    register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对.你想想,一个CPU 的寄存器也就那么几个或几十个,你要是定义了很 ...

  5. Leetcode 之Construct Binary Tree(52)

    根据先序和中序构造二叉树.根据中序和后序构造二叉树,基础题,采用递归的方式解决,两题的方法类似.需要注意的是迭代器的用法. //先序和中序 TreeNode *buildTree(vector< ...

  6. Android-自定义meta-data扩展数据

    在接入第三方渠道SDK的时候,经常会看到其配置文件AndroidManifest.xml有类似如下的定义: [html] view plaincopy <!-- appid --> < ...

  7. ecshop的商品系统数据库整合

    -- 表的结构 `ecs_shop_config` '全站配置信息表'  商店设置   `id`   '全站配置信息自增id',`parent_id`  '父节点id,取值于该表id字段的值',`co ...

  8. ubuntu14.04中国源

    deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://cn.ar ...

  9. 【Other】推荐点好听的钢琴曲

    2013-12-13 16:19 匿名 | 浏览 138977 次 音乐钢琴 推荐点好听的钢琴曲,纯音乐也可以thanks!!! 2013-12-14 19:34 网友采纳 热心网友 巴洛克:帕海贝尔 ...

  10. Linux下配置JDK

    下面以CentOS为例,详细说一下Linux下配置JDK的过程 首先按照约定俗成的习惯,将jdk放在/usr/local/java下,首先进入/usr/local然后新建一个目录java 然后我们需要 ...