↓ Skip to main content

Posts

2016

hdu 3193 find the hotel (思维题)

·403 words·1 min
hdu3193题目链接 题意:给出n个price 和distance,找到一个集合,集合中的每对在全集中找不到比他price和distance都要小的元素。小于是严格的。 思路:一开始以为找到最小值就好。。。结果漏洞百出。。这题还找不到题解。。。大概是太简单了。。? 看了一份代码大概看明白了。。。

linux下的对拍写法

·126 words·1 min
1首先先生成三个程序: 2$ g++ a+b.cpp -o a+b 3$ g++ a+b2.cpp -o a+b2 4$ g++ make.cpp -o make 5然后生成数据 6$ ./make > in.txt 7然后运行两个程序 8$ ./a+b < in.txt > out.txt 9$ ./a+b2 < in.txt > ans.txt 10最后对拍 11$ diff out.txt ans.txt 12输出的结果可以man diff查阅一下相关文档中关于输出含义的内容 13注:上面的$都是命令提示符,复制粘贴时不需要

lightoj 1081 Square Queries (二维rmq,降维)

·434 words·1 min
lightoj 1081 题目链接 题意:和上一道一样,但是由于size变成了500,如果按照之前的做法会tle + mle… 很容易发现,由于是方阵,长宽是相等的,所以有一维是可以省略的。 也就是所谓的降维?

poj 2019 Cornfields (二维rmq)

·497 words·1 min
poj2019题目链接 题意:给一个方阵,k个查询,每个查询求某个方阵的最大值和最小值之差。 思路:二维rmq.同时用到最大值和最小值的话可以把初始化写在一起。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年05月16日 星期一 18时31分23秒 4File Name :code/poj/2019.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=251; 34int a[N][N]; 35int dp[N][N][8][8]; 36int dp2[N][N][8][8]; 37int n,b,q; 38 39void init_rmq() 40{ 41 for ( int i = 1 ;i <= n ; i++) 42 for ( int j = 1 ; j <= n ; j++) 43 dp[i][j][0][0] = dp2[i][j][0][0] = a[i][j]; 44 45 46 for ( int i = 0 ; (1<<i)<= n ; i++) 47 for ( int j = 0 ; (1<<j) <= n ; j++) 48 if (i==0 && j==0) continue; 49 else for ( int p = 1 ; p + (1<<i)-1 <= n ; p++) 50 for ( int q = 1 ; q + (1<<j)-1 <= n ; q++) 51 if (i==0) 52 { 53 dp[p][q][i][j] = max(dp[p][q][i][j-1],dp[p][q+(1<<(j-1))][i][j-1]); 54 dp2[p][q][i][j] = min(dp2[p][q][i][j-1],dp2[p][q+(1<<(j-1))][i][j-1]); 55 } 56 else 57 { 58 dp[p][q][i][j] = max(dp[p][q][i-1][j],dp[p+(1<<(i-1))][q][i-1][j]); 59 dp2[p][q][i][j] = min(dp2[p][q][i-1][j],dp2[p+(1<<(i-1))][q][i-1][j]); 60 } 61} 62 63 64int _rmq(int x1,int y1,int x2,int y2) 65{ 66 int k1 = 0 ; 67 int k2 = 0 ; 68 while (1<<(k1+1)<=x2-x1+1) k1++; 69 while (1<<(k2+1)<=y2-y1+1) k2++; 70 71 int tmp1 = dp[x1][y1][k1][k2]; 72 int tmp2 = dp[x2-(1<<k1)+1][y1][k1][k2]; 73 int tmp3 = dp[x1][y2-(1<<k2)+1][k1][k2]; 74 int tmp4 = dp[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2]; 75 76 int mx = max(max(tmp1,tmp2),max(tmp3,tmp4)); 77 78 tmp1 = dp2[x1][y1][k1][k2]; 79 tmp2 = dp2[x2-(1<<k1)+1][y1][k1][k2]; 80 tmp3 = dp2[x1][y2-(1<<k2)+1][k1][k2]; 81 tmp4 = dp2[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2]; 82 83 int mn = min(min(tmp1,tmp2),min(tmp3,tmp4)); 84 85 // cout<<"mx:"<<mx<<" mn:"<<mn<<endl; 86 87 return mx - mn; 88} 89 90 91int main() 92{ 93 #ifndef ONLINE_JUDGE 94 freopen("code/in.txt","r",stdin); 95 #endif 96 scanf("%d %d %d",&n,&b,&q); 97 for ( int i = 1 ; i <= n ; i++) 98 for ( int j = 1 ; j <= n ; j++) scanf("%d",&a[i][j]); 99 init_rmq(); 100 101 while (q--) 102 { 103 int x1,y1; 104 scanf("%d %d",&x1,&y1); 105 printf("%d\n",_rmq(x1,y1,x1+b-1,y1+b-1)); 106 } 107 108 109 110 #ifndef ONLINE_JUDGE 111 fclose(stdin); 112 #endif 113 return 0; 114}

hdu 2888 check corners (二维rmq模板题)

·484 words·1 min
hdu2888题目链接 题意:问某个矩阵内的最大值,并且问最大值是否是在四个角中出现。 思路:二维rmq.需要注意数组稍微开大1就会MLE,因为是四维数组,一维大一点,整个就会大很多==。

hdu 3183 A Magic Lamp ( 暴力)

·455 words·1 min
hdu3183题目链接 题意:n位长的数字串(n<=1000),删掉m个(m<=n),使得剩下的数字串表示的数字最小。 忽略前导0. 思路:暴力搞就可以。要注意每位数字是有一定位置的范围的。比如当前是第i位数字,后面还要取n-m-i位数字,那么第i位数字最多只能取到第k位,k=m+i,因为这样才能保证后面还有n-m-i位数字。

BZOJ 1636: [Usaco2007 Jan]Balanced Lineup (RMQ模板题)

·1014 words·3 mins
1636: [Usaco2007 Jan]Balanced Lineup # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 680 Solved: 493 [Submit][Status][Discuss] Description # For the daily milking, Farmer John’s N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 <= Q <= 200,000) potential groups of cows and their heights (1 <= height <= 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

BZOJ 1689: [Usaco2005 Open] Muddy roads 泥泞的路 (模拟)

·785 words·2 mins
1689: [Usaco2005 Open] Muddy roads 泥泞的路 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 311 Solved: 227 [Submit][Status][Discuss] Description # Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contains (1 <= N <= 10,000) mud pools. Farmer John has a collection of wooden planks of length L that he can use to bridge these mud pools. He can overlap planks and the ends do not need to be anchored on the ground. However, he must cover each pool completely. Given the mud pools, help FJ figure out the minimum number of planks he needs in order to completely cover all the mud pools.

hdu 4513 吉哥系列故事——完美队形II (回文串,manacher)

·473 words·1 min
题目链接:hdu4513 题意:给出一个n的数的序列,求出一个最长的回文字串,并且满足从[l,mid]单调增(非严格单调,可以相等),[mid,r]单调减(同样是可以相等) 思路:manacher…int型的也是可以搞的。。要求单调的话。。。while扩展的时候判一下就好了。。。

poj 3294 Girls' research (manacher,回文串)

·1257 words·3 mins
poj 3294 题意:先做个简单替换,然后求替换后的字符串的最长回文串,以及这个最长回文串的开始和结束位置。 思路:manacher。需要注意的是,返回下标的时候如果字符串长度为偶数,那么中间是没有字符的,需要特判一下(我的做法是 left+(ans%2==0))。

poj 3974 Palindrome (最长回文字串,manacher裸题)

·297 words·1 min
poj3974 题意:求最大长度的回文字串。 思路:manacher裸题,用来练习算法。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年04月18日 星期一 16时32分25秒 4File Name :code/poj/3974.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=2E6+7; 34char st[2*N]; 35int p[2*N]; 36 37int manacher(char *s) 38{ 39 int len = strlen(s); 40 for ( int i = len ; i >= 0 ; i-- ) 41 { 42 s[i+i+2]=s[i]; 43 s[i+i+1]='#'; 44 } 45 s[0]='$'; 46 47 int id = 0 ; 48 int maxlen = 0 ; 49 50 for ( int i = 2 ; i < 2*len+1 ; i++) 51 { 52 if (id+p[id]>i) p[i] = min(p[2*id-i],id+p[id]-i); 53 else p[i] = 1; 54 55 while (s[i+p[i]]==s[i-p[i]]) p[i]++; 56 if (id+p[id]<i+p[i]) id = i; 57 if (p[i]>maxlen) maxlen = p[i]; 58 } 59 return maxlen-1; 60} 61int main() 62{ 63 #ifndef ONLINE_JUDGE 64 freopen("code/in.txt","r",stdin); 65 #endif 66 int cas = 0 ; 67 while (scanf("%s",st)!=EOF) 68 { 69 if (st[0]=='E'&&st[1]=='N'&&st[2]=='D') break; 70 printf("Case %d: %d\n",++cas,manacher(st)); 71 72 } 73 74 #ifndef ONLINE_JUDGE 75 fclose(stdin); 76 #endif 77 return 0; 78}

BZOJ 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 (单调栈)

·1062 words·3 mins
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 634 Solved: 447 [Submit][Status][Discuss] Description # Farmer John’s N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the range 1..2,000,000,000 nanometers (FJ really is a stickler for precision). Each cow moos at some volume v in the range 1..10,000. This “moo” travels across the row of cows in both directions (except for the end cows, obviously). Curiously, it is heard only by the closest cow in each direction whose height is strictly larger than that of the mooing cow (so each moo will be heard by 0, 1 or 2 other cows, depending on not whether or taller cows exist to the mooing cow’s right or left). The total moo volume heard by given cow is the sum of all the moo volumes v for all cows whose mooing reaches the cow. Since some (presumably taller) cows might be subjected to a very large moo volume, FJ wants to buy earmuffs for the cow whose hearing is most threatened. Please compute the loudest moo volume heard by any cow.

BZOJ 1656: [Usaco2006 Jan] The Grove 树木(神奇的bfs之射线法)

·1177 words·3 mins
1656: [Usaco2006 Jan] The Grove 树木 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 143 Solved: 88 [Submit][Status][Discuss] Description # The pasture contains a small, contiguous grove of trees that has no ‘holes’ in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She’s just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn’t think you could pass ’through’ the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here’s a typical example where ‘.’ is pasture (which Bessie may traverse), ‘X’ is the grove of trees, ‘’ represents Bessie’s start and end position, and ‘+’ marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): …+… ..+X+.. .+XXX+. ..+XXX+ ..+X..+ …+++ The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she’s starting ‘outside’ the grove instead of in a sort of ‘harbor’ that could complicate finding the best path.

BZOJ 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题(01背包)

·758 words·2 mins
3407: [Usaco2009 Oct]Bessie’s Weight Problem 贝茜的体重问题 # Time Limit: 3 Sec Memory Limit: 128 MB Submit: 88 Solved: 79 [Submit][Status][Discuss] Description # 贝茜像她的诸多姊妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干草.贝茜只能吃一整捆干草;当她开始吃一捆干草的之后就再也停不下来了.她有一个完整

BZOJ 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 (母函数,高精度)

·784 words·2 mins
1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 353 Solved: 190 [Submit][Status][Discuss] Description # Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are: 1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1 Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

BZOJ 1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

·560 words·2 mins
1653: [Usaco2006 Feb]Backward Digit Sums # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 349 Solved: 258 [Submit][Status][Discuss] Description # FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this: 3 1 2 4 4 3 6 7 9 16 Behind FJ’s back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ’s mental arithmetic capabilities. Write a program to help FJ play the game and keep up with the cows.