随笔分类 - 题解
题解
摘要:数据不大,直接枚举……嗯,然后就是,比如:1212和1212接龙之后是121212,而不是12121212,就这个注意的,代码:#include <stdio.h>#include <string.h>#include <stdlib.h>char str[20][1001];char tmp[1001];int f[20][20];int used[20];int ans;int n;void srch(int k, int len){ int i; if(len > ans){ ans = len; } used[k]++; for(i = 0;
阅读全文
摘要:可以如果题目有解的话(即,最后一排中没有没有水的城市。),那么第一排中的任意一个城市能够运水到达最后一行的城市是一个区间,是连续的!证明我不写了,我也不会,呵呵。以前看过一篇,然后知道这个信息之后,直接贪心就行了,看代码吧:#include <stdio.h>#include <string.h>#include <stdlib.h>int num[500][500];int can[500][500];int m, n;#define deal(i, j) if((((i) >= 0 && (i) < n &&
阅读全文
摘要:就是排序,唯一麻烦的就是确定先后顺序的函数,就是比较函数啦,嗯,看代码吧:#include <stdio.h>#include <string.h>#include <stdlib.h>char num[20][10001];char tmp[10001];int com(const void *a, const void *b){ char *i = (char *)a, *j = (char *)b; while(*i != '\0' && *i == *j){ i++, j++; } if(*i == '\0
阅读全文
摘要:比较难做的DP吧,首先要预处理一个w[i][j],代表从i~j之间存在的单词个数,嗯。。 然后f[j][i] = max(f[l][i - 1] + w[l + 1][j]) (i<l<j) 代码如下:#include <stdio.h>#include <string.h>#include <stdlib.h>char str[202];char sub[6][201];int len[6];int d[201], w[201][201];int f[201][41];int main(int argc, char **argv){ int i
阅读全文
摘要:这个题目刚开始因为就是枚举,后来数据太大,不能搜,就想到数学方法了,答案就是n/5+n/5^2+n/5^3,知道5^k > n,就不加了。 代码如下:#include <stdio.h>#include <stdlib.h>long long ans;int main(int argc, char **argv){ int i; long long j = 5; long long n; scanf("%I64d", &n); for(i = 1; j <= n; i++){ ans += n / j; j *= 5; } pri
阅读全文
摘要:f[0][i][j]代表不走房子到达i, j的最短路径是多少,f[1][i][j]代表走房子到达i, j的最短路径是多少(并不一定是从i, j走房子)? 然后看我的代码吧,实在不好说……#include <stdio.h>#include <string.h>#include <stdlib.h>#define Q_MAX 100000#define deal(a, b) deal_((a), (b), x, y)int f[2][1000][1000];char map[1000][1000];struct dot{ int x, y;}queue[Q_M
阅读全文
摘要:这题比较悲剧吧,始终只有80分,到最后才发现数组开销了一个1,应该是[101][51],我开的[101][50],然后。。。。80分咯~ 代码如下:#include <stdio.h>#include <stdlib.h>#define min(a, b) (((a)<(b))?(a):(b))int num[101];int f[101][51];int com(const void *a, const void *b){ return *(int *)a - *(int *)b;}int main(int argc, char **argv){ int i,
阅读全文
摘要:就实现正向从1点出发SPFA,获得min[i],就是到达i点能最低购买到的价格,然后反向(将图反向),从n点开始SPFA,获得max[i],就是从i点到终点能够卖出的最大的价格,然后就是寻找差价最大的i,输出答案即可。#include <stdio.h>#include <stdlib.h>int num[100000];int map[1000000], next[1000000];int end;int head[100000];void add(int a, int b){ map[end] = b; next[end] = head[a]; head[a] =
阅读全文
摘要:也不知是怎么,以前没看懂为什么要用二分图,现在就懂了,以后也许碰到类似的题目还会想到用二分图,因为当i<j<k,且num[i]<num[j]&&num[i]>num[k]的时候,肯定是不能将i和j放在同一个栈了(是放在同一个栈,不是同时在一个栈里面。) 嗯,上代码把:#include <stdio.h>#include <stdlib.h>int num[1000];int min[1000];int map[1000000], next[1000000];int end, head[1000];void add(int a, i
阅读全文
摘要:f[i][j]代表将第i束花插进第j个花屏所能获得的最大的分,方程是: f[i][j] = max(f[i - 1][k] + map[i][j]) (i -1=<k<j),代码如下:#include <stdio.h>#include <stdlib.h>int f[101][101];int map[101][101];int p[101][101];void output(int a, int b){ if(a == 1){ printf("%d", b); return; } output(a - 1, p[a][b]); pri
阅读全文
摘要:就是用位运算,直接读出一行中应该放的位置和该放的数字,然后枚举。但不是随意的枚举,要从已经填的最多的那行开始搜,最后搜内容最空白的一行,至于为什么呢?想一想吧,一行中要填的空白多(就是已经填的很少),那每个个子里面可以装的数字就越多,相反,需要填写的很少,那么枚举的情况就少。就是说比如第i行只有一个数字要填,在这一列下面还有几行是空白的,那这一个是确定的,是已知的,只有这一种可能,那在枚举其余行这一列就少了一种可能性,懂了么? 好了,这一个细节纠结了我很久,上代码了:#include <stdio.h>#include <stdlib.h>#define con(i)
阅读全文
摘要:可以用朴素算法,在TYVJ上2000ms,别的评测机可能超时,所以修改成了线段树的,代码如下:#include <stdio.h>#include <stdlib.h>struct tree{ int l, r; int ll, rr; int xor;}tree[100001];int end;int num[50000];int maketree(int s, int t){ int l = end++; tree[l].l = s; tree[l].r = t; if(s == t){ tree[l].ll = tree[l].rr = -1; tree[l].x
阅读全文
摘要:DFS模拟,然后srch(i, j)表示输出用i, j组成的树的后序遍历,返回0就是都是0,1就是都是1,2就是0,1都有。。#include <stdio.h>#include <string.h>#include <stdlib.h>char str[1025];char put[3] = {'B', 'I' ,'F'};int srch(int start, int end){ int i, j; if(start == end){ printf("%c", put[str[start
阅读全文
摘要:数论题目?也许把,不过暴搜在TYVJ的平台上能用(评测机太好了!)因为那个数论的在考试里的话,打死我也是想不到的,所以就模拟了,果断不太多废话:#include <stdio.h>#include <stdlib.h>int pow_(int a, int b){ if(b == 0){ return 1; } if(b & 1){ return (pow_((a * a) % 7, b / 2) * a) % 7; }else{ return (pow_((a * a) % 7, b / 2)) % 7; }}int main(int argc, char *
阅读全文
摘要:终于琢磨出来了强联通分量,看了不少资料啊~~有三种算法,我用的第一种,最慢的,但是也是最普遍的。。 具体讲解看我转的一篇文章吧,代码如下:#include <stdio.h>#include <string.h>#include <stdlib.h>int num[200];int map[200][200];int count[200];int rmap[200][200];int rcount[200];int group[200];int order[200];int flag[200];int now;void srch1(int i){ int j
阅读全文
摘要:类似于二分的算法吧,看代码:#include <stdio.h>#include <stdlib.h>int pow_(int a, int b){ if(b == 0){ return 1; } if(b & 1){ return (pow_((a * a) % 1012, b / 2) * a) % 1012; }else{ return pow_((a * a) % 1012, b / 2) % 1012; }}int main(int argc, char **argv){ int n; int a, b; scanf("%d", &
阅读全文
摘要:暴搜就行,还有一个方法,是logn的算法的,等下看a^b2的代码吧,上暴搜的代码:#include <stdio.h>#include <stdlib.h>int main(int argc, char **argv){ int i; int a, b; int s = 1; scanf("%d%d", &a, &b); for(i = 1; i <= b; i++){ s *= a; s %= 1012; } printf("%d\n", s); return 0;}
阅读全文
摘要:果断直接SPFA,f[i][j]代表到达(i,j)的时间,f[i][j] = f[a][b] + num[i][j] (a, b) 是(i, j)相邻的.. 代码:#include <stdio.h>#include <stdlib.h>int map[25][25];#define MAX 4000struct box{ int x, y;}queue[MAX];int head, rear;int used[25][25];int dis[25][25];int m, n;void enqueue(int x, int y){ if(used[x][y]){ ret
阅读全文
摘要:我用的暴搜,别人说还有一种方法:题目要求n个数同余。等价于求这n个数两两求差(大减小)所得的所有数的最大公约数。 我的暴搜代码如下:#include <stdio.h>#include <stdlib.h>int num[100];int ans;int main(int argc, char **argv){ int i, j, t; int n, max = 0; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%d", &num[i]); if(max &
阅读全文
摘要:简单的模拟吧,大家都尝试尝试~~不太难,代码:#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>char str[101];char c;char *getval(char *str, int *i){ int t = 0, m = 0; if(str[0] == '-'){ m = 1; str++; }else if(str[0] == '+'){ str++; } while(isdigit(*str)){ t
阅读全文
浙公网安备 33010602011771号