2012年5月17日

crontab表达式

Linux下面的crontab表达式是一个很神奇的表达式,几乎所有有意义的时间都能表示出来,最近由于某些原因,也对这个学习了一下。

* * * * * commond

前面的五个星号分别表示 分 时 日 月 周,commond表示你要操作的命令

分(1-59)(*或*/1表示每分钟)

时(1-23)(0表示0点)

日(1-31)

月(1-12)

周(1-6)(0表示周日)

使用方式 :
crontab file [-u user]-用指定的文件替代目前的crontab。
crontab-[-u user]-用标准输入替代目前的crontab.
crontab-1[user]-列出用户目前的crontab.
crontab-e[user]-编辑用户目前的crontab.
crontab-d[user]-删除用户目前的crontab.
crontab-c dir- 指定crontab的目录。
crontab文件的格式:M H D m d cmd.

 

一些crontab的使用例子:

30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启apache

 

这里就介绍一些基础,可以引导入门,但是详细的还得深入学习。

 


                                                                    ------>froest

posted @ 2012-05-17 17:04 牛_ 阅读(894) 评论(1) 编辑

2012年3月28日

堆排序(算法导论)

堆排序:最大堆最小堆以及依赖于最大堆最小堆的最大优先级队列和最小优先级队列。

堆排序不是一种稳定的排序方法,时间复杂度为O(1),空间复杂度为O(n*logn)

以下列出我的创建的方法的声明:

//begin
/*保持大根堆非递归版本O(lgn)*/
void max_heapify(int *a,int i,int num);
/*保持大根堆递归版本O(lgn)*/
void max_heapify1(int *a,int i,int num);
/*创建一个大根堆O(n)*/
void build_max_heapify(int *a,int num);
/*大根堆排序O(nlgn)*/
void max_heap_sort(int *a,int num);
//end

//begin
/*保持小根堆非递归版本O(lgn)*/
void min_heapify(int *a,int i,int num);
/*保持小根堆递归版本O(lgn)*/
void min_heapify1(int *a,int i,int num);
/*创建一个小根堆O(n)*/
void build_min_heapify(int *a,int num);
/*小根堆排序O(nlgn)*/
void min_heap_sort(int *a,int num);
//end

//begin
/*最大优先级队列插入*/
void insertmax(int *a,int x,int num);
/*优先级队列返回最大值*/
int maxinum(int *a);
/*弹出第一个元素*/
int extractmax(int *a,int num);
/*把某个元素的值增加到指定值*/
void increaseMaxKey(int *a,int x,int key);
//end

//begin
/*最小优先级队列插入*/
void insertmin(int *a,int x,int num);
/*优先级队列返回最小值*/
int mininum(int *a);
/*弹出第一个元素*/
int extractmin(int *a,int num);
/*把某个元素的值减小到指定值*/
void increaseMinKey(int *a,int x,int key);
//end

/*打印数组*/
void mprint(int *a,int n); 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//凡是大小根堆都是从a[1]开始保存数据的,因为方便
//关键是保持最大堆的性质

//begin
/*保持大根堆非递归版本O(lgn)*/
void max_heapify(int *a,int i,int num);
/*保持大根堆递归版本O(lgn)*/
void max_heapify1(int *a,int i,int num);
/*创建一个大根堆O(n)*/
void build_max_heapify(int *a,int num);
/*大根堆排序O(nlgn)*/
void max_heap_sort(int *a,int num);
//end

//begin
/*保持小根堆非递归版本O(lgn)*/
void min_heapify(int *a,int i,int num);
/*保持小根堆递归版本O(lgn)*/
void min_heapify1(int *a,int i,int num);
/*创建一个小根堆O(n)*/
void build_min_heapify(int *a,int num);
/*小根堆排序O(nlgn)*/
void min_heap_sort(int *a,int num);
//end

//begin
/*最大优先级队列插入*/
void insertmax(int *a,int x,int num);
/*优先级队列返回最大值*/
int maxinum(int *a);
/*弹出第一个元素*/
int extractmax(int *a,int num);
/*把某个元素的值增加到指定值*/
void increaseMaxKey(int *a,int x,int key);
//end

//begin
/*最小优先级队列插入*/
void insertmin(int *a,int x,int num);
/*优先级队列返回最小值*/
int mininum(int *a);
/*弹出第一个元素*/
int extractmin(int *a,int num);
/*把某个元素的值减小到指定值*/
void increaseMinKey(int *a,int x,int key);
//end

/*打印数组*/
void mprint(int *a,int n);

int main(){
    freopen("F://学习//算法//codeblock//11//in.txt","r",stdin);
    int n,a[100];
    scanf("%d",&n);
    int i;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    printf("打印原始数组数据:\n");
    mprint(a,n);
    build_max_heapify(a,n);
    printf("打印创建的大根堆数组数据:\n");
    mprint(a,n);
    max_heap_sort(a,n);
    printf("打印大根堆排序后的数组数据:\n");
    mprint(a,n);
    printf("====================================\n");
    printf("打印打印插入一个元素后大根堆的数组数据:\n");
    insertmax(a,100,n);
    n=n+1;
    mprint(a,n);
    printf("打印大根堆的最大的数据:\n");
    printf("%d\n",maxinum(a));
    printf("打印大根堆弹出最大元素后的数组数据:\n");
    int x=extractmax(a,n);
    printf("%d\n",x);
    n=n-1;
    mprint(a,n);
    printf("打印大根堆第五个元素增大到101后的数组数据:\n");
    increaseMaxKey(a,5,101);
    mprint(a,n);
    printf("====================================\n");
    build_min_heapify(a,n);
    printf("打印创建的小根堆数组数据:\n");
    mprint(a,n);
    min_heap_sort(a,n);
    printf("打印小根堆排序后的数组数据:\n");
    mprint(a,n);
    printf("====================================\n");
    printf("打印小根堆第4个元素减小到7后的数组数据:\n");
    build_min_heapify(a,n);//先使数组变成最小堆
    mprint(a,n);//打印数组变成最小堆之后的数据
    increaseMinKey(a,4,7);
    mprint(a,n);
    insertmin(a,2,n);
    printf("打印小根堆插入一个元素后的数组数据:\n");
    n+=1;
    mprint(a,n);
    printf("打印小根堆最小的元素数据:\n");
    printf("%d\n",mininum(a));
    printf("打印弹出小根堆最小的元素后的数组数据:\n");
    int min=extractmin(a,n);
    printf("%d\n",min);
    mprint(a,n);
    //freopen("F://学习//算法//codeblock//11//out.txt","w",stdout);
    //fclose(stdout);
    return 0;
}
/*保持大根堆非递归版本*/
void max_heapify(int *a,int i,int num){
    int max=0;
    while(i<=num/2){
        int l=2*i,r=2*i+1;
        if(l<=num&&a[l]>a[i]){
            max=l;
        }else{
            max=i;
        }
        if(r<=num&&a[r]>a[max]){
            max=r;
        }
        if(i!=max){
            int temp=a[i];
            a[i]=a[max];
            a[max]=temp;
            i=max;
        }else{
            break;
        }
    }
}
/*保持大根堆递归版本*/
void max_heapify1(int *a,int i,int num){
    int l=2*i,r=2*i+1;
    int max=0;
    if(l<=num&&a[l]>a[i]){
        max=l;
    }else{
        max=i;
    }
    if(r<=num&&a[r]>a[max]){
        max=r;
    }
    if(i!=max){
        int temp=a[i];
        a[i]=a[max];
        a[max]=temp;
        max_heapify1(a,max,num);
    }
}
/*创建一个大根堆*/
void build_max_heapify(int *a,int num){
    int i;
    for(i=num/2;i>=1;i--){
        max_heapify(a,i,num);
    }
}
/*大根堆排序*/
void max_heap_sort(int *a,int num){
    int i;
    for(i=num;i>=2;){
        int temp=a[i];
        a[i]=a[1];
        a[1]=temp;
        max_heapify(a,1,--i);
    }
}
/*保持小根堆非递归版本*/
void min_heapify(int *a,int i,int num){
    int min=0;
    while(i<=num/2){
        int l=2*i,r=2*i+1;
        if(l<=num&&a[l]<a[i]){
            min=l;
        }else{
            min=i;
        }
        if(r<=num&&a[r]<a[min]){
            min=r;
        }
        if(i!=min){
            int temp=a[i];
            a[i]=a[min];
            a[min]=temp;
            i=min;
        }else{
            break;
        }
    }
}
/*保持小根堆递归版本*/
void min_heapify1(int *a,int i,int num){
    int l=2*i,r=2*i+1,min=0;
    if(l<=num&&a[l]<a[i]){
        min=l;
    }else{
        min=i;
    }
    if(r<=num&&a[r]<a[min]){
        min=r;
    }
    if(min!=i){
        int temp=a[i];
        a[i]=a[min];
        a[min]=temp;
        min_heapify1(a,min,num);
    }
}
/*创建一个小根堆*/
void build_min_heapify(int *a,int num){
    int i;
    for(i=num/2;i>=1;i--){
        min_heapify(a,i,num);
    }
}
/*小根堆排序*/
void min_heap_sort(int *a,int num){
    int i;
    for(i=num;i>=2;){
        int temp=a[i];
        a[i]=a[1];
        a[1]=temp;
        min_heapify(a,1,--i);
    }
}
/*优先级队列插入*/
void insertmax(int *a,int x,int num){
    num+=1;
    a[num]=-0x7fffffff;
    increaseMaxKey(a,num,x);
}
/*优先级队列返回最大值*/
int maxinum(int *a){
    return a[1];
}
/*弹出第一个元素*/
int extractmax(int *a,int num){
    int max=a[1];
    a[1]=a[num];
    num-=1;
    max_heapify(a,1,num);
    return max;
}
/*把某个元素的值增加到指定值*/
void increaseMaxKey(int *a,int i,int key){
    if(key<=a[i]){
        printf("error\n");
    }
    a[i]=key;
    while(i/2>=1&&a[i/2]<a[i]){
        int t=a[i/2];
        a[i/2]=a[i];
        a[i]=t;
        i=i/2;
    }
}
/*最小优先级队列插入*/
void insertmin(int *a,int x,int num){
    num+=1;
    a[num]=0x7fffffff;
    increaseMinKey(a,num,x);
}
/*优先级队列返回最小值*/
int mininum(int *a){
    return a[1];
}
/*弹出第一个元素*/
int extractmin(int *a,int num){
    int min=a[1];
    a[1]=a[num];
    num-=1;
    min_heapify(a,1,num);
    return min;
}
/*把某个元素的值减小到指定值*/
void increaseMinKey(int *a,int x,int key){
    if(key>=a[x]){
        printf("error\n");
    }
    a[x]=key;
    while(x/2>=1&&a[x/2]>a[x]){
        int t=a[x/2];
        a[x/2]=a[x];
        a[x]=t;
        x=x/2;
    }
}
/*打印数组*/
void mprint(int *a,int n){
    int i;
    for(i=1;i<=n;i++){
        printf("%d ",a[i]);
    }
    printf("\n");
}

 

    以最大堆为例,关键方法是保持最大堆的性质(方法 max_heapify1()),即父亲节点的值必须比子节点要大。假设父节点i,则他的子节点为2*i和2*i+1,找出这三个节点中值最大的,父节点与这个最大的节点交换,如果最大的节点就是父节点或者这个节点的子节点都不是父节点,那么方法调用结束,否则递归调用该方法。

    创建一个大根堆,只需要从第i=num/2开始的节点调用 max_heapify1(),并且把该节点的父节点赋给i,并且继续调用 max_heapify1(),直到i=1,结束该方法的调用。至于为什么是从第num/2个节点开始的,是因为二叉树的性质,总共有num个节点,那么最后一个父节点就是num/2。

    最大堆排序,只需要把最后一个节点和第一个节点交换,然后对第一个节点调用一次 max_heapify1()来保持最大堆的性质,最后的输出顺序是从小到大输出。

    最大堆最小堆类似,最大优先级队列以及最小优先级队列都是基于最大堆和最小堆来实现的,理解了最大堆,可以很轻松的理解其他三个。

 

 

My cnblogs is :
 
http://www.cnblogs.com/God-froest/

 

 Welcome you to technology exchange with me.

         
     《
死の舞踏

                        ---Maksim Mrvica

   Piano music can develop the temperament of a man.

 

 

 

 

posted @ 2012-03-28 10:18 牛_ 阅读(338) 评论(0) 编辑

2012年3月27日

快速排序(算法导论中的版本)

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <fstream>
#include <utility>
#include <iomanip>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
#include <limits.h>
using namespace std;
//算法导论中描述的快速排序,随机化版本
int partition(int *a,int p,int r){
    int x,i=p-1,j,t;
    j=rand()%(r-p)+p;
    t=a[r];
    a[r]=a[j];
    a[j]=t;
    x=a[r];
    for(j=p;j<r;j++){
        if(a[j]<=x){
            i+=1;
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    t=a[i+1];
    a[i+1]=a[r];
    a[r]=t;
    return i+1;
}

void quicksort(int *a,int p,int r){
    if(p<r){
        int q=partition(a,p,r);
        quicksort(a,p,q-1);
        quicksort(a,q+1,r);
    }
}

int main()
{
    int a[]={1,3,2,5,9,4,3};
    quicksort(a,0,6);
    for(int i=0;i<7;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

 算法导论学习,实现快速排序,时间复杂度(nlogn)

posted @ 2012-03-27 22:46 牛_ 阅读(23) 评论(0) 编辑

2012年3月8日

POJ2632

模拟robot,用一个二维数组来存放各个robot的位置和方向,但是注意二维数组的方向和实际的相差90°,理解的时候需转换

 

代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct p{
int x;
int y;
int c;
}P;
int main()
{
freopen("F://学习//算法//codeblock//11//in.txt","r",stdin);
//freopen("F://学习//算法//codeblock//11//in.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--){
int a,b,n,m;
scanf("%d%d%d%d",&a,&b,&n,&m);
int f[101][101];
memset(f,0,sizeof(f));
int i;
char c1;
P r[101];
for(i=1;i<=n;i++){
scanf("%d%d %c",&r[i].x,&r[i].y,&c1);
if(c1=='N'){
r[i].c=0;
}else if(c1=='W'){
r[i].c=1;
}else if(c1=='S'){
r[i].c=2;
}else{
r[i].c=3;
}
f[r[i].x][r[i].y]=i;
}
int j,cnt;
bool flag=false,flag1=false;
for(i=1;i<=m;i++){
scanf("%d %c%d",&j,&c1,&cnt);
if(flag||flag1){
continue;
}
if(c1=='L'){
cnt=cnt%4;
r[j].c=(r[j].c+cnt)%4;
}else if(c1=='R'){
cnt=cnt%4;
r[j].c=(r[j].c-cnt+4)%4;
}else{
int k;
if(r[j].c==0){
for(k=r[j].y+1;k<=b&&k<=r[j].y+cnt;k++){
if(f[r[j].x][k]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[r[j].x][k]);
flag=true;
break;
}
}
if(!flag){
if(r[j].y+cnt>b){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x][r[j].y+cnt]=j;
f[r[j].x][r[j].y]=0;
r[j].y+=cnt;
}
}else if(r[j].c==1){
for(k=r[j].x-1;k>=1&&k>=r[j].x-cnt;k--){
if(f[k][r[j].y]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[k][r[j].y]);
flag=true;
break;
}
}
if(!flag){
if(r[j].x-cnt<1){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x-cnt][r[j].y]=j;
f[r[j].x][r[j].y]=0;
r[j].x-=cnt;
}
}else if(r[j].c==2){
for(k=r[j].y-1;k>=1&&k>=r[j].y-cnt;k--){
if(f[r[j].x][k]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[r[j].x][k]);
flag=true;
break;
}
}
if(!flag){
if(r[j].y-cnt<1){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x][r[j].y-cnt]=j;
f[r[j].x][r[j].y]=0;
r[j].y-=cnt;
}
}else{
for(k=r[j].x+1;k<=a&&k<=r[j].x+cnt;k++){
if(f[k][r[j].y]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[k][r[j].y]);
flag=true;
break;
}
}
if(!flag){
if(r[j].x+cnt>a){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x+cnt][r[j].y]=j;
f[r[j].x][r[j].y]=0;
r[j].x+=cnt;
}
}
}
}
if(!flag&&!flag1){
printf("OK\n");
}
}
return 0;
}


 

posted @ 2012-03-08 16:48 牛_ 阅读(16) 评论(0) 编辑

2012年3月7日

CodeForcesDiv2第111专场C题

此题题意是给你n个数以及一个k,两两总共有n2 种组合,(p1,q1)<(p2,q2)当p1<p1或者p1=p2,q1<q2两种情况时,如果把所有组合都列出来求出第k个组合,那么肯定超时。所以另觅他径,把n的数排序,考虑相等的情况,比如1 1 1 2 2 5,i = 1,j = 4时,a[i]!=a[j] 此时判断k是否在(i-1)*n和(j-1)*n之间,如果不在,则i=j,否则p1=a[i],p2=a[(k-(i-1)*n)/(j-i)+1],此处3个1有9种组合。

 注意(i-1)*n,(j-1)*n的值会超出int范围,有2中做法解决:1.把表达式中的n强制转换成(__int64)类型;2.在输入n值的时候直接申明n的类型为__int64。一个表达式如果左右两边的类型不匹配的时候,类型小的会强制转换成类型大的。

 

代码如下:

 1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 #include <map>
5 #include <queue>
6 #include <set>
7 #include <fstream>
8 #include <utility>
9 #include <iomanip>
10 #include <stack>
11 #include <list>
12 #include <vector>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <cstring>
16 #include <cmath>
17 #include <ctime>
18 #include <ctype.h>
19 #include <limits.h>
20 using namespace std;
21 int cmp1(const void* a,const void* b){
22 return (*(int*)a)-(*(int*)b);
23 }
24
25 int main()
26 {
27 freopen("F://学习//算法//codeblock//11//in111.txt","r",stdin);
28 //freopen("F://学习//算法//codeblock//11//in.txt","w",stdout);
29 int n;
30 __int64 k;
31 int a[100005];
32 scanf("%d%I64d",&n,&k);
33 //memset(a,0,sizeof(a));
34 int i,j;
35 for(i=1;i<=n;i++){
36 scanf("%d",&a[i]);
37 }
38 qsort(a+1,n,sizeof(a[0]),cmp1);
39 j=1;
40 for(i=1;i<=n;){
41 while(j<=n&&a[i]==a[j]){
42 j++;
43 };
44 //i-1在运算前会转换成n的类型,如果n是int,那么乘积可能溢出,导致随机数,所以应该先把n强制转换成__int64类型
45 //或者在定义n的时候用__int64 类型接收
46 if(k>(__int64)(i-1)*n&&k<=(__int64)(j-1)*n){
47 printf("%d %d\n",a[i],a[(k-(__int64)(i-1)*n-1)/(j-i)+1]);
48 }
49 i=j;
50 }
51 return 0;
52 }


                                                                          ------->froest


posted @ 2012-03-07 16:14 牛_ 阅读(16) 评论(0) 编辑

2011年12月11日

20111211

摘要: This days my nabs come my house and we did some interesting things just like learning the techonology of hack and we solve some questions about the hack. I find I have many things to learn. ----->froest阅读全文

posted @ 2011-12-11 13:14 牛_ 阅读(12) 评论(0)  编辑

2011年12月3日

20111203today

摘要: This day I watched the film named <Lock, Stock and Two Smoking Barrels>,I thought the style of this film like <Crazy stone>.There are many coincidences in series of the whole story.And at last,the heroes got nothing,but fortunately,they are all alive,life is the most important. Afternoon阅读全文

posted @ 2011-12-03 23:19 牛_ 阅读(3) 评论(2)  编辑

20111203

摘要: It was several days I hadn't written the diary,because there was nothing to record except work. Yesterday,my nabs pao sended me messages which said that he quarrelled with his parents about where he should be go to learn computer science.And Yesterday night ,he spend all the night at net cafe.To阅读全文

posted @ 2011-12-03 00:19 牛_ 阅读(8) 评论(0)  编辑

2011年11月24日

20111124

摘要: This day I finished the problem about two stacks to achieving the function of queue. I talked with a net friend about my emotion.And I felt at ease.Thank you for you. ----->froest阅读全文

posted @ 2011-11-24 21:35 牛_ 阅读(1) 评论(0)  编辑

2011年11月23日

20111123

摘要: This was a black day.I was felt worse,and my heart was deep pain.Tears was flowing in my heart.No body knew about my heart.If you look back,I'll leave and I'll get away from you,get out of your world. ----->froest阅读全文

posted @ 2011-11-23 21:56 牛_ 阅读(1) 评论(0)  编辑

仅列出标题  下一页

导航

统计

公告