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. LayoutComponent类,用于layout的组件类。 LayoutComponent保存的所有用于布局的数据。

      LayoutComponent ()   默认构造函数 更多...     ~LayoutComponent ()   默认的析构函数 更多...     CREATE_FUNC (LayoutC ...

  2. 添加删除一个controller

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) rails generate controller Users rails destroy cont ...

  3. No bootable device-insert boot disk and press any key

    macbook air 2012 mid. 长按关机键关机,按开机键,然后长按option键,会出现可以选择启动的磁盘块,选择要启动的磁盘进入即可.

  4. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  5. linux ldconfig

    http://blog.csdn.net/dante_k7/article/details/7211868 ldconfig的主要用途: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/l ...

  6. HTML5 manifest离线缓存

    一.基本概念 离线缓存是HTML5新引入的技术,能够让你的Web应用程序指定哪些文件可以缓存在本地,使得你的网络断开时依然可以通过本地的缓存来进行访问浏览. 二.使用方法 1. MIME type 声 ...

  7. 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...

  8. iOS 使用Storyboard 和 xib时的一些知识

    以前不太使用xib和storyboard进行布局,后来在工作中参与到了一个项目的维护工作,那个项目就是使用stroyboard的,再加上xcode5对stroyboard的大力支持,就在这里对于使用s ...

  9. Java中成员变量和局部变量的区别

    java面向对象过程中,最基本的两类变量就是成员变量和局部变量 成员变量是写在类中并且写在方法外部,一般写在每个类的头部,用于初始化或者方法操作,作用域是整个类被实例化到被销毁,中间变量都可以被外部方 ...

  10. VS添加lib库

    #pragma comment(lib,"opengl32.lib")