随笔分类 - OJ--USACO
摘要:为了这题还去学了下迭代加深 回来还是不会写只好参考各大神的代码及题解了二分枚举最大可以切的块数 然后就是各种分析及优化USACO题解里写了7个优化。。问题分析抽象一下就可以发现,算法的本质是多重背包问题。 补充:这题与破锣乐队都是多个背包,不可重复放物品。区别在于破锣乐队要有顺序,此题不需要,这样此题就必须要搜索才行。 单个背包的问题我们可以用DP解决,但是对于这种问题我们只能用搜索了。 但是可以看一看这道题的数据规模:1 max{board} 那么这个rail应该舍去 By Clarkok 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK:...
阅读全文
摘要:最近脑子有点乱 老是不想清楚就啪啪的敲 敲完之后一看 咦。。样例都过不去 仔细一想 这样不对啊刚开始就写了一SPFA 最后发现边跟点的关系没处理好 删了。。写dfs。。还是没转化好 开始搜解题方法 看到别人都说最小环 解最小环的方法跟我想的差不多 就是边转化点没处理好又想了想 开个二维数组标记下 转化点 然后就枚举每条边的两个点间的最短距离 要先删了这条边 找个最小的就好了dijk求最短路 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: fence6 5 */ 6 #include 7 #include 8 #include 9 #i...
阅读全文
摘要:昨天晚上写的一题 结果USACO一直挂中 今天交了下有一点点的数论知识 背包很好想 就是不好确定上界官方题解:这是一个背包问题。一般使用动态规划求解。一种具体的实现是:用一个线性表储存所有的节点是否可以相加得到的状态,然后每次可以通过一个可以相加得到的节点,通过加上一个输入的数求出新的可以相加得到的点。复杂度是O(N×结果)。但是可以证明结果不会超过最大的两个数的最小公倍数(如果有的话)。参见数论。所以复杂度也是O(Na2),完全可以接受了。判断无限解可以按上面的方法,另外也可以算所有的数的最大公约数。如果不是1,也就是说这些数不互质,那么不被这个最大公约数整除的数一定构造不出来。当
阅读全文
摘要:USACO挂了一小时。。我坚持不懈的等。。终于打开了 把3章最后一题交了 可以安心的睡去了之前题意没看清楚 不知道要有序 写了一状压 结果TLE了 再优化也TLE 后来想写状态转移时发现 它必须有序放。。这下就简单多了枚举所有最后状态 找个最大值就OK 了。。汗。。我刚交上 又打不开了 还好交得快 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: rockers 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 int...
阅读全文
摘要:忘记pick定理是什么了 想枚举来着 。。没枚出来有篇pick定理的证明 貌似挺好 也没太看懂 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: fence9 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 int gcd(int x,int y)13 {14 return y==0?x:gcd(y,x%y);15 }16 int main()17 {18 freopen("fence9.in","r...
阅读全文
摘要:已知中前 求后序递归一下 有一些小细节 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: heritage 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 char s1[30],s2[30];13 int o,kk;14 void order(int s,int e)15 {16 int i,k;17 if(s>=kk)18 return ;19 if(s>=e)20 ret...
阅读全文
摘要:这题水的真不易。。300多行 累死了 对着数据查错啊枚举每个边上的点到源点 是否中间隔着别的边 每条边划分500份就够了 注意一下与源点在一条直线上的边不算几何 啊,,好繁琐 参考各种模版。。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: fence4 5 */ 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 using namespace std; 13 typedef struct pointt 14 { 15 ...
阅读全文
摘要:刚开始理解有点误,想成每步都是最优的 ,结果卡在第六组数据上,,无奈瞧了下别人的 发现自己理解错了,,我看的还是中文翻译。。简单的记忆化 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: game1 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 int a[210],dp[210][210],s[210];13 int find(int i,int j)14 {15 16 if(i==j)17 {1...
阅读全文
摘要:之前做过一道类似的 国际象棋盘神马的。。统计出以每个1作为右下角的最大正方形 那么以大于二到这个最大值之间为边的正方形都可以以这个为右下角 累加就可以了dp[i][j] = min(dp[i-1][j],dp[i-1][j-1],dp[i][j-1])+1; 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: range 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 int dp[255][255],num[255]...
阅读全文
摘要:恶心的题啊 。。先枚举哪个点是所有人集合的点 再枚举所有骑士遇见国王的点 如果全部枚举出来会大大的TLE 经大牛验证 只需要枚举国王周围的点就可以了+-2 之内然后各种繁琐 各种错误 骑士有可能不带着国王一块走 也可能在他周围选个点带着走 先预处理出来每个骑士到国王周围的最短距离 然后再按上面的枚举就可以了考虑的不全面 。。错了好多个样例 样例2,6,19,20 都模拟了一遍。。还好错在小数据上 可以手算模拟一下 就知道哪错了 变量名都被我用穷了。。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: camelot 5 */ ...
阅读全文
摘要:五维DP,听着挺多的,貌似就是挺裸的dp,最近貌似做简单的DP挺顺手。。1Adp[i][j][e][o][g] = min(dp[i][j][e][o][g],dp[i-i1][j-i2][e-i3][o-i4][g-i5]+p[q]) i1,i2...为满足给出的商品数量的值 p[q]为选用当前优惠方案的价格。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: shopping 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace s...
阅读全文
摘要:都忘了欧拉路径是什么了。。用dfs搜 标记边 刚开始直接从I-N搜 直接超时 2了 先找符合起点和终点的点搜 即度数是奇数d单dfs也超了 后来换了个姿势。。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: fence 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 int w[510][510],n,p[2010],flag,m,de[510],t;13 void dfs(int u,int d)14 {15 ...
阅读全文
摘要:直接枚举角度 数据比较水吧 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: spin 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 struct node13 {14 int l[10],r[10],k,v;15 }p[10];16 int f[6][370];17 int main()18 {19 freopen("spin.in","r",stdin);20 freopen("spin.
阅读全文
摘要:最短路复杂度估计错误 以为SPFA是N*m的 用了dij超时 用SPFA直接跑就好了O(k*e) K 一般为2,3; 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: butter 5 */ 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 using namespace std; 13 #define M 1500 14 #define P 850 15 #define N 550 16 #define INF 0xfffff...
阅读全文
摘要:1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: msquare 5 */ 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #define INF 0xfffffff 13 using namespace std; 14 int o[10]; 15 typedef struct node 16 { 17 int num; 18 int a[10],pre; 19 char c; 20 }st; 21 ...
阅读全文
摘要:直接枚举到100水过 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: ratios 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 #define INF 0xfffffff13 int a[10][10];14 int main()15 {16 freopen("ratios.in","r",stdin);17 freopen("ratios.out","w",
阅读全文
摘要:DP预处理出来 i位不超过j的个数 然后再进行从小到大找第一个比I大的 然后用I减掉上一个比I小的 剩余的按照之前的方法循环找 知道剩余为0细节挺繁琐的 对照数据改了又改 最后一组数据还超 了int。。 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: kimbits 5 */ 6 #include 7 #include 8 #include 9 #include10 #include11 using namespace std;12 #define LL long long13 LL dp[32][32],kk[32];14 void...
阅读全文
摘要:枚举 1 #include 2 /* 3 ID: shangca2 4 LANG: C++ 5 TASK: fact4 6 */ 7 #include 8 #include 9 #include10 11 using namespace std;12 int di[5000];13 int main()14 {15 freopen("fact4.in","r",stdin);16 freopen("fact4.out","w",stdout);17 int i,j,k,n,g=0;18 di[0] = 1;19 c
阅读全文
摘要:对dp很无奈。。枚举所有可能达到的值 dp[i]表示到达i值所用最少的邮票 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: stamps 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<stdlib.h>10 #include<algorithm>11 using namespace std;12 int p[55],dp[2000010];13 bool o[2000010];14 int mai
阅读全文
摘要:又从cuishen 那学了新东西。。map是可以输出的。。还有这个字符串的输入方式挺高端的 学习了。。之后就是暴力枚举就行了 1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: contact 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm>10 #include<stdlib.h>11 #include<cmath>12 #include<map>13 #i
阅读全文

浙公网安备 33010602011771号