USACO Packing Rectangles

这题真BT…….
题解:http://starforever.blog.hexun.com/2097115_d.html
自己最开始毫无头绪,上网搜了题解。。
最后一种情况要仔细考虑,每个矩形还要翻转。。
还要注意输出。

/*
ID: djgreen1
LANG: C
PROB: packrec
*/
#include <stdio.h>
#include <stdbool.h>
struct box
{
    int h,w;
};
struct box rectangle[6],rect[6],ans[6];
bool used[6],ro[6];
int p,s=10000,w,h;
 
int max(int a,int b)
{
    if (a>b) return a;
    else return b;
}
void judge()
{
    int i,j,temp;
    if (w>h)
    {
        temp=w;
        w=h;
        h=temp;
    }    
    if (w*h<=s)
    {
        if (w*h==s) 
        {
            for (i=1;i<=p;i++) 
            if (w==ans[i].w) return;
            for (i=1;i<=p;i++)
            if (w<ans[i].w)
            {
                for (j=p+1;j>i;j--)ans[j]=ans[j-1];
                ans[i].w=w;
                ans[i].h=h;
                p++;
                return;
            }
            p++;
            ans[p].h=h;
            ans[p].w=w;
        }    
        else
        {
            s=w*h;
            p=1;
            ans[p].h=h;
            ans[p].w=w;
        }
    }
}
void work()
{    
    //Case 1
    w=rect[1].w+rect[2].w+rect[3].w+rect[4].w;
    h=max(rect[1].h,rect[2].h);
    h=max(h,rect[3].h);
    h=max(h,rect[4].h);
    judge();
 
    //Case 2
    w=max(rect[1].w+rect[2].w+rect[3].w,rect[4].w);
    h=max(rect[1].h,rect[2].h);
    h=max(h,rect[3].h);
    h+=rect[4].h;
    judge();
 
    //Case 3
    w=max(rect[1].w+rect[2].w,rect[3].w)+rect[4].w;
    h=max(max(rect[1].h,rect[2].h)+rect[3].h,rect[4].h);
    judge();
 
    //Case 4
    w=rect[1].w+max(rect[2].w,rect[3].w)+rect[4].w;
    h=max(rect[1].h,rect[4].h);
    h=max(h,rect[2].h+rect[3].h);
    judge();
 
    //Case 6
    if (rect[3].h>=rect[2].h+rect[4].h)
    {
        w=max(rect[3].w+rect[2].w,rect[3].w+rect[4].w);
        w=max(w,rect[1].w);
        h=rect[1].h+rect[3].h;
        judge();
        return;
    }
    if (rect[3].h>rect[4].h)
    {
        w=max(rect[1].w+rect[2].w,rect[2].w+rect[3].w);
        w=max(w,rect[4].w+rect[3].w); 
        h=max(rect[1].h+rect[3].h,rect[2].h+rect[4].h);
        judge();
        return;
    }
    if (rect[3].h==rect[4].h)
    {
        w=max(rect[1].w+rect[2].w,rect[3].w+rect[4].w);
        h=max(rect[1].h,rect[2].h)+rect[3].h;
        judge();
        return;
    }
    if (rect[3].h<rect[4].h && rect[4].h<rect[3].h+rect[1].h)
    {
        w=max(rect[1].w+rect[2].w,rect[1].w+rect[4].w);
        w=max(w,rect[3].w+rect[4].w);
        h=max(rect[1].h+rect[3].h,rect[2].h+rect[4].h);
        judge();
        return;
    }
    w=max(rect[2].w,rect[1].w+rect[4].w);
    w=max(w,rect[3].w+rect[4].w);
    h=rect[4].h+rect[2].h;
    judge();    
}
void exchange(int i)
{
    int temp;
    temp=rect[i].h;
    rect[i].h=rect[i].w;
    rect[i].w=temp;
}    
void rotate(int depth)
{
    if (depth==5)
    {
        work();
        return;
    }
    if (!ro[depth])
    {
        ro[depth]=true;
        exchange(depth);
        rotate(depth+1);
        ro[depth]=false;
        exchange(depth);
        rotate(depth+1);
    }    
}        
void search(int depth)
{
    int i;
    if (depth==5)
    {
        rotate(1);
        return;
    }
    for (i=1;i<5;i++)
    {
        if (!used[i])
        {
            rect[depth]=rectangle[i];
            used[i]=true;
            search(depth+1);
            used[i]=false;
        }
    }
}                
 
int main()
{
    int i;
    FILE *in=fopen("packrec.in","r"),*out=fopen("packrec.out","w");
 
    memset(ans,0,sizeof(ans));
    for (i=1;i<5;i++) fscanf(in,"%d%d",&rectangle[i].h,&rectangle[i].w);
    fclose(in);
    search(1);
    fprintf(out,"%d\n",s);
    for (i=1;i<=p;i++)
    {
        fprintf(out,"%d %d\n",ans[i].w,ans[i].h);
    }
    fclose(out);
}

USACO Prime Cryptarithm

额 字母很乱。。。

     i j k
   x   a b
_______________
     e d c
   h g f
_______________
   n m l
/*
ID: djgreen1
LANG: C
PROB: crypt1
*/
#include <stdio.h>
#include <stdbool.h>
 
int main()
{
    int i,j,k,a,b,c,d,e,f,g,h,l,m,n,ans=0,num;
    bool digit[10];
    FILE *in=fopen("crypt1.in","r"),*out=fopen("crypt1.out","w");
    fscanf(in,"%d",&num);
    memset(digit,0,sizeof(digit));
    for (i=0;i<num;i++) 
    {
        fscanf(in,"%d",&j);
        digit[j]=true;
    }
    for (i=1;i<10;i++)
    {
        if (!digit[i]) continue;
        for (j=0;j<10;j++)
        {
            if (!digit[j]) continue;
            for (k=0;k<10;k++)
            {
                if (!digit[k]) continue;
                for (a=1;a<10;a++)
                {
                    if (!digit[a]) continue;
                    for (b=0;b<10;b++)
                    {
                        if (!digit[b]) continue;
                        c=k*b;
                        d=c/10;
                        c=c%10;
                        if (!digit[c]) continue;
                        d+=b*j;
                        e=d/10;
                        d=d%10;
                        if (!digit[d]) continue;                        
                        e+=b*i;
                        if (e>9 || e==0 || !digit[e]) continue;
 
                        f=a*k;
                        g=f/10;
                        f=f%10;
                        if (!digit[f]) continue;
                        g+=a*j;
                        h=g/10;
                        g=g%10;
                        if (!digit[g]) continue; 
                        h+=a*i;
                        if (h>9 || h==0 || !digit[h]) continue;
 
                        l=d+f;
                        m=l/10;
                        l=l%10;
                        if (!digit[l]) continue;
                        m+=e+g;
                        n=m/10;
                        m=m%10;
                        if (!digit[m]) continue;
                        n+=h;
                        if (n>9 || n==0 || !digit[n]) continue;
 
                        ans++;
                    }
                }
            }
        }
    }
    fprintf(out,"%d\n",ans);
    fclose(in);
    fclose(out);
}

USACO Calf Flac

从前面一个一个搜就行,枚举回文串的中点,注意奇偶两种情况。
Test 1: TEST OK [0.000 secs, 1956 KB]
Test 2: TEST OK [0.000 secs, 1952 KB]
Test 3: TEST OK [0.011 secs, 1948 KB]
Test 4: TEST OK [0.000 secs, 1948 KB]
Test 5: TEST OK [0.011 secs, 1952 KB]
Test 6: TEST OK [0.011 secs, 1952 KB]
Test 7: TEST OK [0.011 secs, 1952 KB]
Test 8: TEST OK [0.076 secs, 1952 KB]
今天折腾了好久,好几个IDE都弄不好。。


/*
ID: djgreen1
LANG: C
PROB: calfflac
*/
#include <stdio.h>
#include <stdbool.h>
bool compare(char ch1,char ch2)
{
    if (ch1==ch2 || (int)ch1+32==ch2 || (int)ch2+32==ch1) return true;
    else return false;
}
int main()
{
    char code[20001],temp,real[20001];
    int i,j,sum,a,length,ans,tempbegin,tempend,p[20001];
    FILE *in=fopen("calfflac.in","r"),*out=fopen("calfflac.out","w");
    i=0;
    j=0;
    while (fscanf(in,"%c",&code[i])!=EOF)
    {
        if (((int)code[i]>64 && (int)code[i]<91) || ((int)code[i]>96 && (int)code[i]<123))
        {
            real[j]=code[i];
            p[j]=i;
            j++;
        }
        i++;
    }
    ans=0;
    for (a=0;a<j;a++)
    {
        length=1;
        while (a-length>=0 && a+length<j)
        {
            if (compare(real[a-length],real[a+length])) length++;
            else break;
        }
        if (length*2-1>ans)
        {
            ans=length*2-1;
            tempbegin=a-length+1;
            tempend=a+length-1;
        }
        length=0;
        while (a-length>=0 && a+length+1<j)
        {
            if (compare(real[a-length],real[a+length+1])) length++;
            else break;
        }
        if (length*2>ans) 
        {
            ans=length*2;
            tempbegin=a-length+1;
            tempend=a+length;
        }
    }
    fprintf(out,"%d\n",ans);
    for(a=p[tempbegin];a<=p[tempend];a++) fprintf(out,"%c",code[a]);
    fprintf(out,"\n");
    fclose(in);
    fclose(out);
}

谷歌再见

谷歌为什么要退出:

  1. 极光行动英语:Operation Aurora) 或欧若拉行动是2009年12月中旬可能源自中国的 一场网络攻击(英语:cyber attack[1], 其名称“Aurora”(意为极光欧若拉)来自攻击者电脑上恶意文件所在路径的一部分[2]。 遭受攻击的除了Google[3]外, 还有20多家公司:其中包括Adobe Systems[4]Juniper Networks[5]Rackspace[6]雅虎赛门铁克诺斯洛普·格鲁门陶氏化工[7]。 这场攻击过后,Google提出了它的新计划:它将“在必要的法律范围内”[8], 于中国运营一个完全不受过滤的搜索引擎;同时Google也承认,如果该计划不可实现,它将可能离开中国并关闭它在中国的办事处[3]。 via 维基百科
  1. 有证据表明数十位与中国有关的人权活动人士的Gmail帐户经常被第三方侵入
  2. 过去一年中试图进一步限制在华网络言论自由的企图,包括持续不断的屏蔽Facebook、Twitter、YouTube、Google Docs和Blogger等网站
  3. 中国政府已经非常明确地表示,自我审查是一项没有商量余地的法律要求。

2, 3, 4 来自谷歌声明原文华尔街日报译稿更新:Google官方中文版声明

“据当地法律法规和政策,部分搜索结果未予显示” ——谁知道是哪部法律哪条规定了哪些结果不能显示
在哪个国家,访问某些网站会出现连接超时连接被重置
在哪个国家,建立个人网站需要备案,需至IDC处当面拍照核验身份等备案资料才可能予以通过
在哪个国家,没有100万就不要在网上开论坛?(海子铁路网、铁路在线论坛等等)
在哪个国家,查处一个网站,会株连整个机房
在哪个国家,域名可以被随意停止解析
在哪个国家,工信部会花四千万采购绿坝

译言、BlogBus活了过来,Yo2已死。其实还有很多网站。。

图片拍摄于2010年1月15日 清华科技园谷歌中国办事处

www.google.com.hk 其实和 www.google.com的简体中文搜索没什么区别吧,中国大陆访问Google香港仍存在关键词重置,显然Google香港在墙外。
“www.google.com.hk”会成为敏感词吗?谁知道呢?“你是互联网,我是防火墙”,防火墙会存在多久呢?
“我们计划继续在中国的研发工作” 希望谷歌拼音和谷歌音乐搜索能留住^_^

译言被封时看到:“当初我们学英语是为了了解世界,如今我们学英语是为了了解中国。”

PS:Tweet: 您的QQ帐号受到临时登录限制!RT: @rtmeme: RT @Gackiliang RT @4zai: 大家把QQ签名改成 “欢迎您来到谷歌搜索在中国的新家”,改完后下线,再上线,看看有啥效果?

//在网上发言要注意人身安全,包括但不限于QQ、MSN、Tom-Skype、各大论坛、校内等SNS。。

PS:香港政府:不会审查网络内容,尊重资讯自由??
PS:中国大陆对Google各项服务封锁
PS:2006年的谷歌宣传动画

Google在2006年推出Google.cn时就说“Filtering our search results clearly compromises our mission”。为了用户的安全,Google在中国没有存放用户数据,因此谷歌音乐甚至不能用Gmail账户登录
遗憾的是,谷歌的离开没能让所有中国网民了解事情的经过,国内媒体不得不报道谷歌的负面新闻。

今天是2010年3月23日。

Update @ 10:32 2010/3/24 疑似google.com.hk/search成为敏感词。

Update @ 10:42 2010/3/24 现已恢复正常

Update @ 23:41 2010/3/24 #323candlelight 音频

USACO Barn Repair

这题做得真纠结,第五次才AC。
刚想起来其实排序可以优化一点点。。
输入数据不是有序的,要先排序!我的算法就是先铺上木板再去掉最大的那几个空隙。
前后改来改去,写得很乱。还要注意m很大的情况。

/*
ID: djgreen1
LANG: C
PROB: barn1
*/
#include <stdio.h>
 
int main()
{
    int m,s,c,num[200],data[200],first,i,j,p=1,pnum=0,a,sum,gap,t;
    FILE *in=fopen("barn1.in","r"),*out=fopen("barn1.out","w");
 
    fscanf(in,"%d%d%d",&m,&s,&c);
    for(i=1;i<=c;i++) fscanf(in,"%d",&data[i]);
    //sort
    for(i=1;i<c;i++)
    for(j=1;j<c;j++) if (data[j]>data[j+1])
    {
        t=data[j+1];
        data[j+1]=data[j];
        data[j]=t;
    }    
    first=data[1];
    a=first;
    for(i=2;i<=c;i++)
    {
        if (data[i]>i-p+first)
        {
            gap=data[i]-(i-p+first);
            num[pnum]=gap;
            pnum++;
            first=data[i];
            p=i;
        }
    }
 
    //sort
    for(i=pnum-1;i>0;i--)
    for(j=pnum-1;j>0;j--) if (num[j]>num[j-1])
    {
        t=num[j-1];
        num[j-1]=num[j];
        num[j]=t;
    }    
    sum=data[c]-a+1;
    if (m>pnum+1) m=pnum+1;
    for(i=0;i<m-1;i++) sum=sum-num[i];
    fprintf(out,"%d\n",sum);
    fclose(in);
    fclose(out);
}

USACO Mixing Milk

快排+贪心,还要注意0 0的情况。。

/*
ID: djgreen1
LANG: C
PROB: milk
*/
#include <stdio.h>
long a[5000],p[5000];
void qsort(int l,int r)
{
	int i,j,pivot;
	long temp;
	i=l;
	j=r;
	pivot=p[rand()%(r-l+1)+l];
	while (i<j)
	{
		while (p[i]<pivot) i++;
		while (p[j]>pivot) j--;
		if (i<=j)
		{
			temp=p[i];
			p[i]=p[j];
			p[j]=temp;
			temp=a[i];
			a[i]=a[j];
			a[j]=temp;
			i++;
			j--;
		}
	}
	if (j>l) qsort(l,j);
	if (i<r) qsort(i,r);
}
int main()
{
	FILE *in=fopen("milk.in","r"),*out=fopen("milk.out","w");
	long n,m,i,milk=0,price=0;
 
	fscanf(in,"%d%d",&n,&m);
	if (n==0)
	{
	    fprintf(out,"0\n");
	    fclose(out);
	    return 0;
	}
	for(i=0;i<m;i++) fscanf(in,"%d%d",&p[i],&a[i]);
	fclose(in);
	srand(time(NULL));
	qsort(0,m-1);
	for(i=0;i<m;i++)
	{
	    if (milk+a[i]<n)
	    {
	        milk+=a[i];
	        price+=p[i]*a[i];
	    }
	    else
	    {
	        price+=(n-milk)*p[i];
	        break;
	    }
	}
	fprintf(out,"%d\n",price);
	fclose(out);
}

USACO Dual Palindromes

昨天做的,本来以为很快就能打完。原来是“if (ispal()) sum++;”忘记打ispal的括号了。。调了很久,ispal返回值一直是一个固定的大数。调好后USACO就挂了。。

/*
ID: djgreen1
LANG: C
PROB: dualpal
*/
#include <stdio.h>
#include <stdbool.h>
 
int p,new[20];
void convert(int num, int base)
{
    p=0;
    while (true)
    {
        new[p]=num%base;
        num=num/base;
        if (num==0) break;
        p++;
    }
}
bool ispal()
{
    int j;
    for (j=0;j<=p;j++)
    {
        if (new[j]!=new[p-j])
        {
            return(0);
        }
    }
    return(1);
}
int main()
{
    FILE *in=fopen("dualpal.in","r"),*out=fopen("dualpal.out","w");
    int n,s,sum,find=0,i;
 
    fscanf(in,"%d%d",&n,&s);
    while (find<n)
    {
        s++;
        sum=0;
        for (i=2;i<=10;i++)
        {
            convert(s,i);
            if (ispal()) sum++;
            if (sum==2)
            {
                fprintf(out,"%d\n",s);
                find++;
                break;
            }
        }
    }
    fclose(in);
    fclose(out);
    return 0;
}