码迷,mamicode.com
首页 > 其他好文 > 详细

水题集

时间:2015-05-03 09:24:07      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:水题

刻苦练习CCF

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1585

就是旋转一下,找对应关系,注意这里枚举的顺序按原来顺序来,然后输出的时候把n和m的内外顺序换一下

#include<stdio.h> 
int a[105][105],b[105][105];
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int T,q,n,m;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&q);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&a[i][j]);
			}
		}
		if(q==1){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=m;j++){
					b[j][n-i+1]=a[i][j];
				}
			}
			for(int i=1;i<=m;i++){
				for(int j=1;j<=n;j++){
					//b[i][j]=a[m-j+1][i];
					if(j==n) printf("%d\n",b[i][j]);
					else printf("%d ",b[i][j]);
				}
			}
		}
		else{
			for(int i=1;i<=n;i++){
				for(int j=1;j<=m;j++){
					b[m-j+1][i]=a[i][j];
				}
			}
			for(int i=1;i<=m;i++){
				for(int j=1;j<=n;j++){
					if(j==n) printf("%d\n",b[i][j]);
					else printf("%d ",b[i][j]);
				}
			}
		}
	}
}

合并果子

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1588

每次优先合并最小的两个数,其实就是哈夫曼树的建法,用一个优先队列维护

#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;

int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	priority_queue<int,vector<int>,greater<int> > pq;
	int T,n,tmp;
	scanf("%d",&T);
	while(T--){
		int s=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++) {
			scanf("%d",&tmp);
			pq.push(tmp);
		}
		int t1,t2;
		for(int i=1;i<n;i++){
			t1=pq.top();
			pq.pop();
			t2=pq.top();
			pq.pop();
			s+=t1+t2;
			pq.push(t1+t2);
		}
		pq.pop();
		printf("%d\n",s);
	}
	
}

烦人的异或:

用一个数组该点到左上角的异或总值

#include<stdio.h> 
#include<algorithm>
#include<string.h>
using namespace std;
int s[1005][1005],l[1005][1005],h[1005][1005];
int T,n,m,q;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&q);
		memset(s,0,sizeof(s));
		memset(l,0,sizeof(l));
		memset(h,0,sizeof(h));
		int tmp;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf("%d",&tmp);
				l[i][j]=l[i][j-1]^tmp;
				h[i][j]=h[i-1][j]^tmp;
				s[i][j]=s[i-1][j-1]^tmp^l[i][j-1]^h[i-1][j];
			}
		}
		for(int i=1;i<=m;i++){
			//printf("%d %d\n",s[1][i],l[1][i]);
		}
		int x1,y1,x2,y2;
		while(q--){
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			int sum=0;
			sum^=s[x1-1][y1-1];
			sum^=s[x1-1][m]^s[x1-1][y2];
			sum^=s[x2][y2]^s[x1-1][y1-1]^(s[x1-1][y2]^s[x1-1][y1-1])^(s[x2][y1-1]^s[x1-1][y1-1]);
			sum^=s[n][y1-1]^s[x2][y1-1];
			sum^=s[n][m]^s[x2][y2]^(s[x2][m]^s[x2][y2])^(s[n][y2]^s[x2][y2]);
			printf("%d\n",sum);
		}
		
	}
}

括号匹配:

这道题,一开始理解错了,给出的已经括号是符合运算表达式的括号,所以直接用个栈来维护就行了

#include<stack>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) -x&x
using namespace std;
const int maxn=110000;
const int INF=0x3f3f3f3f;

char str[maxn];
int match[maxn];
int tree[3][maxn];
void insert(int *a,int x){
    while(x<maxn){
        a[x] ++;
        x += lowbit(x);
    }
}
int get_sum(int *a,int x){
    int res = 0;
    while(x>0){
        res += a[x];
        x -= lowbit(x);
    }
    return res;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
    while(~scanf("%s",str+1)){
        stack<int> ss;
        int s,m,l,n;
        s=m=l=0;
        memset(tree,0,sizeof(tree));
        m=strlen(str+1);
        for(int i = 1;i<=m;i++){
            if(str[i]=='(' || str[i]=='[' || str[i]=='{')
                ss.push(i);
            else{
                int t = ss.top();
                ss.pop();
                match[t]=i;
                match[i]=t;
                if(str[i]==')') insert(tree[0],t);
                else if(str[i]==']') insert(tree[1],t);
                else if(str[i]=='}') insert(tree[2],t);
            }

        }
        scanf("%d",&n);
        for(int i = 0;i<n;i++){
            int t;
            scanf("%d",&t);
            int c=match[t];
            //printf("%d %d\n",get_sum(tree[0],c),get_sum(tree[0],t));
            printf("%d",c);
            if(match[t]<t) c=t,t=match[t];
            c--;
            printf(" %d %d %d\n",get_sum(tree[0],c)-get_sum(tree[0],t),get_sum(tree[1],c)-get_sum(tree[1],t),get_sum(tree[2],c)-get_sum(tree[2],t));
        }
        printf("\n");
    }

    return 0;
}


水题集

标签:水题

原文地址:http://blog.csdn.net/lj94093/article/details/45443143

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!