
思维
hao_zong_yin
有问题可以加QQ讨论2987048728,备注一下优快云
展开
-
UVA 11549 Calculator Conundrum——flody判圈算法
非常有意思的一个题目,暴力枚举的思路很好想,但是时间空间复杂度都很高,这时候就可以同时开启两条线路,增长率分别为1和2,当线路1和线路2再次相等时敲好走过了一个周期,在走这个周期的过程中维护最大值ans就可以了#include <cstdio>#include <cstring>#include <iostream>#include <algorit...原创 2018-03-06 22:06:57 · 218 阅读 · 0 评论 -
UVA 11997 K Smallest Sums——多路归并
一开始没想出来,看了蓝书的题解才懂的。详解参考蓝书P190#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int maxn = 800;int...原创 2018-03-07 19:52:25 · 183 阅读 · 0 评论 -
UVA 11925 Generating Permutations——构造
问如何把给定123.。。n通过两个操作变成给定序列,首先逆向思考一下,从给定序列变成123.。。n比较简单,同时第二个操作变成从尾拿到首,然后根据排序的思想,比较前两个,若是逆序则交换,然后把最后一个拿到第一个重复这个过程,直到序列为正序,注意如果第一个是n第二个是1则不交换,只有这一个特判。#include <cstdio>#include <cstring>#inc...原创 2018-03-01 15:13:02 · 244 阅读 · 0 评论 -
UVA 1339 Ancient Cipher——快速排序
算法:字母可以重排,因此字母的位置并不重要,重要的是字母出现的次数;因此我们可以用两个数组a[26],b[26]分别记录两个字符串中各字母出现的次数,然后进行排序,若排序之后的结果相同,则证明两个字符串之间存在一一映射关系;注意,题目需要调用stdlib.h里的快速排序函数qsort,int comInc(const void *a, const void *b) {原创 2016-12-04 20:50:35 · 905 阅读 · 4 评论 -
UVA 133 The Dole Queue——思路题
紫书上的例题,解释一下我程序中的p = (p + n + d) % n:这条语句其实就是帮你把超出边界的情况考虑了,不用再加 if 判断了;没越过边界时,式子商0余(p+n+d),即为正确结果;越过边界时,式子商>=1(循环的圈数),余数相当于又从初始位置开始走m或k步进行挑选,因此还是能得到正确结果;PS:紫书上是p = (p + n + d) % n + 1,我这里在程序中做了原创 2016-12-05 11:56:16 · 437 阅读 · 1 评论 -
UVA 213 Message Decoding——思路题
紫书例题,具体参考紫书P83;顺便补充一点小知识:代码中的1#include #include #include int readchar(){ while(1){ int ch = getchar(); if(ch != '\n' && ch != '\r') return ch;//一直读到非换行符为止 }}int r原创 2016-12-05 21:56:56 · 484 阅读 · 1 评论 -
UVA 1589 Xiangqi——模拟
模拟题,我的做法是先让黑将分别上下左右移动,对每一种情况 先判断是否能被马将死(列举8种情况),然后判断是否能被 将 车 炮 将死(以黑将为中心十字展开,判断是否有 将 车 炮)注意:1.开局两将正对红方必输;2.吃子情况;3.输入问题(因为输入错了好多次)#include #include #include #include int main(){//原创 2016-12-21 09:10:54 · 868 阅读 · 1 评论 -
UVA 201 Squares——思路题
按照边长从小到大输出,不是个数,因为这个错了一遍。。。#include #include #include int main(){ int squar[20][20]; int n, m, flag = 0; while( scanf("%d %d",&n, &m) == 2 ){getchar(); memset(squar,0,size原创 2016-12-22 11:28:45 · 435 阅读 · 0 评论 -
UVA 220 Othello——模拟
1的问题: {多1少1} 是程序员经常犯的错误,写判断条件时一定要注意写i、i-1、i+1中的哪一个,、>=中的哪一个;然后再来看这道题,算法不难,关键是细节,忽视了任何一个小细节就可能wa,注意:1 输入问题,注意getchar吃\n;2 边界问题,处理时保证不越界,推荐写一个函数专门判断;3 执行M时别忘了把新的棋子放到棋盘上;4 只有两子之间的棋子才会原创 2016-12-25 12:59:07 · 431 阅读 · 0 评论 -
UV 442 Matrix Chain Multiplication——思路题
#include #include #include #include #include using namespace std;struct matrix{ int a, b; matrix(int x = 0, int y = 0){//构造函数初始化a、b a = x, b = y; }}m[26];int main(){原创 2017-03-04 18:40:29 · 242 阅读 · 0 评论 -
插入排序详解
有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序法,插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。插入算法把要排序的数组分成两部分:第一部分包含了这个数组的所有元素,但将最后一个元原创 2017-03-12 10:24:06 · 346 阅读 · 0 评论 -
归并排序详解
分治法在每层递归时都有三个步骤:1.分解原问题为若干个子问题,这些子问题是原问题规模较小的实例;2.解决子问题,递归求解各子问题,如果子问题规模足够小,则直接求解;3.合并子问题得到原问题的解。归并排序完全遵循分治法:1.分解待排序的n个元素的序列,分成各具有n/2个元素的两个子序列;2.使用归并排序递归地排序两个子序列;3.合并两个已排序的子序列得到已排序的答原创 2017-03-12 11:29:46 · 458 阅读 · 1 评论 -
POJ 369 Meteor Shower——bfs
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a sa原创 2017-03-13 15:50:15 · 326 阅读 · 0 评论 -
POJ 3273 Monthly Expense——二分
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that h原创 2017-03-13 17:43:02 · 286 阅读 · 0 评论 -
基于八数码问题的hash判重
数据结构:数据间关系+数据存储方式选择何种数据结构,取决于需要解决什么样的问题,任何一个数据结构都有它的优势,这个优势说白了就是“本数据结构在进行XX操作时快”,而选择何种数据结构就看要解决的问题需要在数据结构上进行何种操作来决定,哈希表就是体现这个道理的一个很好的例子。哈希表提供这么一种其它数据结构并不擅长的操作:“在理想情况下,能用常量时间查找到指定值的数据”。普通原创 2017-03-16 11:57:17 · 1817 阅读 · 0 评论 -
素数筛法详解
我们直接抓住素筛的核心:定理: 如果n不是素数, 则n有满足1证明:I1. 如果n不是素数, 则n有满足1I2. 如果d是素数, 则定理得证, 算法终止.I3. 令n=d, 并转到步骤I1.由于不可能无限分解n的因子, 因此上述证明的算法最终会停止.代码:// primes[i]是递增的素数序列: 2, 3, 5, 7, ...// 更准确地说primes[i]序原创 2017-03-16 12:00:46 · 608 阅读 · 0 评论 -
快速排序详解
快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常被采用,再加上快速排序思想----分治法也确实实用,因此很多软件公司的笔试面试,包括像腾讯,微软等知名IT公司都喜欢考这个,还有大大小的程序方面的考试如软考,考研中也常常出现快速排序的身影。总的说来,要直接默写出快速排序还是有一定难度的,因为本人就自己的理解对快速排序作了下白话解释,希望对大家理解有帮助,达到快速排序原创 2017-03-21 16:29:28 · 326 阅读 · 0 评论 -
棋盘覆盖问题
#include using namespace std;const int maxn = 2000;int graph[maxn][maxn];int ans;int px, py;//棋盘大小以及黑块的位置void dfs(int x, int y, int len) { if(len == 2) { ans++; return; } ans++;//每次递归原创 2017-03-21 20:35:19 · 300 阅读 · 0 评论 -
巨人与鬼问题
#include #include #include #include #include #include using namespace std;const int maxn = 110;int n, ans[maxn];struct Node { int x, y, id; double angle; void creat_angle(const原创 2017-03-23 16:13:52 · 633 阅读 · 0 评论 -
UVA 120 Stacks of Flapjacks ——思路题
#include #include #include #include using namespace std;int stacks[1000], operation[1000];int cnts = 0, cnto = 0;void solve(int lower_edge) { if (lower_edge <= 1) return; int maxp =原创 2017-03-23 21:23:37 · 433 阅读 · 0 评论 -
HDU 5546 Ancient Go——dfs
#include #include #include #include using namespace std;const int dx[] = {0, 0, -1, 1};const int dy[] = {-1, 1, 0, 0};char graph[10][10];int save[10][10], save_point[10][10];int cnt;bool e原创 2017-04-05 21:46:33 · 379 阅读 · 3 评论 -
HDU 5867 Water problem——模拟
#include #include #include #include #include #include using namespace std;const int solve1[] = {0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8};const int solve2[] = {0, 3, 6, 6,原创 2017-04-09 20:52:24 · 322 阅读 · 0 评论 -
HDU 5857 Median——模拟
#include #include #include using namespace std;int n,m;int a[100010];int l1,r1,l2,r2;int solve(int len){ if(r1 <= l2) { if(r1-l1+1 >= len) return a[l1+len-1]; else原创 2017-04-09 21:41:16 · 334 阅读 · 0 评论 -
UVA 1604 立体八数码问题——编码解码+哈希+dfs
双广度优先搜索+状态压缩+hash去重这题关键是一点一点把上面三个模块写好,我的模块仅供参考:1.开头到分割线是状态压缩,这里我用了9位10进制编码压缩了状态图,用了一个二维数组维护了各种旋转状态,每个人的写法各异,代码仅供参考2.两条分割线之间是hash表,采用了邻接表的形式实现了完美哈希表,其中ok是是否插入元素的开关,毕竟判断路径相遇时不能插入元素3.后面的部分是双广度有限搜原创 2017-04-28 11:01:15 · 830 阅读 · 0 评论 -
POJ 1064 Cable master——二分 + 精度控制
白书上的原题,对浮点数进行二分,注意保留两位小数不是四舍五入,2.579应该保留2.57感觉这题没什么意思,为坑而坑,交G++错了,交C++就对了,貌似和scanf的判定有关#include #include #include #include #include using namespace std;const int maxn = 100000 + 10;int N,原创 2017-05-20 13:25:25 · 344 阅读 · 0 评论 -
lower_bound/upper_bound使用方法
设要查找的序列为增序lower_bound的作用是二分查找求下界,即在前闭后开区间【first,last)中查找值value,返回等于或大于value的第一个位置,若所有元素都小于value,则返回lastupper_bound的作用是二分查找求上界,即在前闭后开区间【first,last)中查找值value,返回大于value的第一个位置,若所有元素都小于value,则返回last原创 2017-05-23 18:55:37 · 436 阅读 · 0 评论 -
UVa 11292 Dragon of Loowater——思路题
#include #include #include #include using namespace std;const int maxn = 20000 + 10;int a[maxn], b[maxn];int main(){ int n, m; while (scanf("%d %d", &n, &m) == 2 && n + m) {原创 2017-05-25 18:18:12 · 233 阅读 · 0 评论 -
UVa 11300 Spreading the Wealth——中位数
有n个人,我们要求的是转移硬币的最小值,为此可以求出每个人转移硬币的数量,然后相加便是转移硬币的总数量设总硬币数为sum,最终每个人都拥有m个硬币(m = sum / n)设这n个人所持有的硬币数为ai,每个人给上一个人的硬币数为xi(注意:1.xi可以为负数,2.第一个人给最后一个人 )现在我们就要求 |xi| 的最小总和所以对于第一个人来说: m = a1 - x1 + x2原创 2017-05-26 16:27:23 · 321 阅读 · 0 评论 -
UVa 10881 Piotr's Ants ——思路题
#include #include #include #include using namespace std;const int maxn = 10000 + 5;const char dirName[][10] = {"L", "Turning", "R"};struct Art { int id, pos, sta; bool operator < (co原创 2017-05-26 18:59:27 · 249 阅读 · 0 评论 -
POJ 3104 Drying ——二分
#include #include #include #include using namespace std;const int maxn = 100000 + 10;int n, a[maxn], k, l, r, ans;bool solve(int t) { int sum = 0; for (int i = 1; i <= n; i++) {原创 2017-05-26 23:03:41 · 271 阅读 · 0 评论 -
UVa 11464 Even Parity——思路题
只枚举第一行便可根据第一行推出整个矩阵注意n = 1输出0#include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;int n, ans;int a[20][20], b[20][20];void solve() { ans = INF; int原创 2017-05-27 11:06:42 · 294 阅读 · 0 评论 -
UVa 11384 Help is needed for Dexter——思路题
f(n) = f( n / 2) + 1可以借助uDebug的数据来辅助思考#include #include #include #include using namespace std;int solve(int n) { if (n == 1) return 1; return solve(n / 2) + 1;}int main(){原创 2017-05-27 16:03:17 · 287 阅读 · 0 评论 -
UVa 1610 Party Games——细节处理
大体思路是用string存储字符串,从小到大进行排序,因为n是偶数所以直接选出中间两个字符串left和right来计算结果。从left的第一个元素开始,先把他+1,然后累加到ans中,如果此时ans小于right,那么ans一定是最优解,直接跳出循环输出结果。如果ans大于或等于right,说明+1后ans大了,把他-1回到正常值,然后继续循环。。。。。。然后要注意细节,首先+1时不能原创 2017-05-30 10:45:29 · 350 阅读 · 0 评论 -
UVa 1605 Building for UN ——思路题
#include #include using namespace std;const char ans[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";int main(){ int n; while (scanf("%d", &n) == 1) { printf("2 %d %d原创 2017-06-01 17:00:26 · 272 阅读 · 0 评论 -
UVa 1152 4 Values whose Sum is 0——二分
灵活使用STL#include #include #include #include using namespace std;const int maxn = 4000 + 10;int a[maxn], b[maxn], c[maxn], d[maxn], x[maxn*maxn];int main(){ int T, n; cin >> T; w原创 2017-06-01 17:02:12 · 269 阅读 · 0 评论 -
UVa 11572 Unique Snowflakes——思路题
这个题划分一下子问题就很好理解了设ans【i】是以第i个元素为首的最长集合的长度,那么求解过程便是对于每一个i,尽量将其无限延长,直到出现重复元素为止,那么结果便是所有ans【i】的最大值实际操起来时,我们可以用【L, R)表示一个序列的区间,开始L = R = 0,然后将R无限延长直到出现重复元素,这时直接将L + 1,然后继续无限延长R,出现重复元素后再把L + 1....原创 2017-06-01 20:48:27 · 252 阅读 · 0 评论 -
UVa 1606 Amphiphilic Carbon Molecules——极角扫描
#include #include #include #include #include using namespace std;const int maxn = 1010;struct Node { int x, y, col; double rad; bool operator < (const Node &node) { retur原创 2017-06-03 10:51:32 · 300 阅读 · 0 评论 -
UVa 1451 Average——斜率优化
思路紫书上写的就很好,这里只强调一点:注意要求的斜率是(Sj - Si-1)/(j - i + 1),一定要注意这个i-1,假如想得到前三个数的平均值,在图上就要从0开始,求0到3的平均值,而不是1到三的平均值。说的有点抽象,还请读者琢磨一下代码里的+1-1的问题#include #include #include #include using namespace std;原创 2017-06-05 18:42:00 · 516 阅读 · 0 评论 -
UVa 714 Copying Books——二分最大值最小化
注意上界在累加时可能超过int范围#include #include #include #include using namespace std;int m, k;long long a[1000];bool judge(long long x) { long long sum = 0, cnt = 1; for (int i = m; i >= 1; i-原创 2017-06-05 20:52:27 · 336 阅读 · 0 评论 -
Uva 11093 Just Finish it up——思路题
#include #include using namespace std;const int maxn = 100000 + 10;int n, a[maxn], b[maxn];int main(){ int T, flag = 0, i, j, sum; scanf("%d", &T); while (T--) { scanf("%d", &原创 2017-06-07 17:37:29 · 323 阅读 · 0 评论