重拾ZOJ 一周解题
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 一周解题的更多相关文章
- 重拾《 两周自制脚本语言 》- Eclipse插件实现语法高亮
源码库: program-in-chinese/stone-editor-eclipse 参考: FAQ How do I write an editor for my own language? D ...
- CSS魔法堂:重拾Border之——更广阔的遐想
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- CSS魔法堂:重拾Border之——不仅仅是圆角
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- CSS魔法堂:重拾Border之——图片作边框
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- CSS魔法堂:重拾Border之——解构Border
前言 当CSS3推出border-radius属性时我们是那么欣喜若狂啊,一想到终于不用再添加额外元素来模拟圆角了,但发现border-radius还分水平半径和垂直半径,然后又发现border-t ...
- 重拾Blog
上个月是我入职现在的公司三周年的月份,所以又续订了五年的合同,最近有一些思考,也不知道这个五年能否还会一直在这个公司工作. 一切随缘吧. 闲适有毒,忙碌的时光总是过的很快,自从加入这个公司以来,日常的 ...
- [linux]重拾linux
起因 因为想重拾起linux,同时需要用docker起几个镜像,用来学习网络知识.本来想直接去阿里云上买,后来一想自己机器上,起一个linux是个不错的选择,毕竟不花钱! 还可以用来做本地测试,学习使 ...
- 重拾qt
最近公司又接了一个煤矿的项目,要写个小程序摘取数据,我是公司唯一c++程序员,本来搞ios搞好好的,现在又得重拾半年没摸得qt了.呵呵...呵呵呵. 这里只记录这次小程序的一些小的总结吧.. 1.中文 ...
- 重拾linux
重拾linux 起因 因为想重拾起linux,同时需要用docker起几个镜像,用来学习网络知识.本来想直接去阿里云上买,后来一想自己机器上,起一个linux是个不错的选择,毕竟不花钱! 还可以用来做 ...
随机推荐
- 我所理解cocos2d-x 3.6 lua --使用Cocos Studio
Cocos是触控科技推出的游戏开发一站式解决方案,包含了从新建立项.游戏制作.到打包上线的全套流程. 开发者可以通过cocos快速生成代码.编辑资源和动画,最终输出适合于多个平台的游戏产品. Coco ...
- 在spring中获取代理对象代理的目标对象工具类
昨天晚上一哥们需要获取代理对象的目标对象,查找了文档发现没有相应的工具类,因此自己写了一个分享给大家.能获取JDK动态代理/CGLIB代理对象代理的目标对象. 问题描述:: 我现在遇到个棘手的问题,要 ...
- [Effective JavaScript 笔记] 第14条:当心命名函数表达式笨拙的作用域
js函数会根据上下文改变其含义. function double(x){return x*2;} 这是一个函数声明,也可以是一个命名函数表达式(named function expression),取 ...
- OpenSwitch操作系统成为Linux基金会官方项目
导读 非盈利机构Linux基金会为推进Linux和开源软件在企业和专业人士的发展,于今天宣布OpenSwitch项目成为Linux基金会官方项目之一. Linux基金会的常务董事Jim Zemlin表 ...
- HTML快速入门3
四.表格 (Table) 1. 表格的基本形式 表由 <table> 开始, </table> 结束,表的内容由 <tr>,<th> 和 <td& ...
- error: library dfftpack has Fortran sources but no Fortran compiler found解决方法
用pip install scipy 时提示 error: library dfftpack has Fortran sources but no Fortran compiler found 解决方 ...
- 推荐一个linux下的web压力测试工具神器webbench
推荐一个linux下的web压力测试工具神器webbench2014-04-30 09:35:29 来源: 评论:0 点击:880 用多了apache的ab工具之后你就会发现ab存在很多问题, ...
- 细微之处:比较两种CSS清除浮动的兼容
http://www.cnblogs.com/bienfantaisie/archive/2011/05/27/2059597.html 清除浮动是连续浮动元素之后的必备工作,在工作中我做到需要清除浮 ...
- error splicing file: file too large解决方法
FAT32格式的usb最大支持4G的文件,拷贝超过4G的文件需要把usb换成NTFS格式.
- 【分布式存储】GlusterFS failing to mount at boot with Ubuntu 14.04
GlusterFS failing to mount at boot with Ubuntu 14.04 Previously I asked about mounting GlusterFS a ...