/ zzf / 讨论 / 题解 /

暂存

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n,a[40][40];

int main(void){
    
    scanf("%d",&n);
    int posi=0,posj=n/2;

    for(int i=0;i<n*n;i++){
        while(a[posi][posj]>0){
            posi=(posi+n-1)%n;
            posj=(posj+1)%n;
        }
        a[posi][posj]=i+1;
        printf("%d %d %d\n",i+1,posi,posj);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            printf("%d ",a[i][j]);
        printf("\n");   
    }
        
    return 0;
}

8 条评论

  • @ 2018-12-03 19:40:04
    #include <cstdlib>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define N 1001
    #define For(i,l,r) for(int i=l;i<=r;i++)
    using namespace std;
    struct zzf{
        int cnt;
        int pos;
    };
    int m,n,k,l,d;
    zzf x[N],y[N];
    
    bool cmp(zzf a,zzf b){
        if(a.cnt<b.cnt){
            swap(a.pos,b.pos);
            return a.cnt<b.cnt;
        }else{
            return a.cnt<b.cnt;
        }
    }
        
    int main(void){
        scanf("%d%d%d%d%d",&m,&n,&k,&l,&d);
        For(i,1,d){
            int xi,yi,pi,qi;
            scanf("%d%d%d%d",&xi,&yi,&pi,&qi);
            if(!(xi-pi)) {
                x[xi].cnt++;
                x[xi].pos=xi;   
            }else{
                y[yi].cnt++;
                y[yi].pos=yi;
            }
        }
        sort(x,x+m,cmp);
        sort(y,y+n,cmp);
        For(i,1,m) printf("%d ",x[i].cnt);
        printf("\n");
        For(i,1,n) printf("%d ",y[i].cnt);
        
        return 0;
    }
    

    1056

  • @ 2018-10-17 19:59:59

    线段树模板

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define N 100001
    using namespace std;
    
    struct zzf{
        int left,right;
        long long val,lazy;
    }tree[N];
    int n,m;
    
    void push_down(int root){
        if(tree[root].lazy){
            tree[root<<1].lazy=tree[root].lazy;
            tree[root<<1|1].lazy=tree[root].lazy;
            tree[root<<1].val+=(tree[root<<1].right-tree[root<<1].left+1)*tree[root].lazy;
            tree[root<<1|1].val+=(tree[root<<1|1].right-tree[root<<1|1].left+1)*tree[root].lazy;
            tree[root].lazy=0; 
        }
    }
    
    void updata(int root,int l,int r,int add){
        if(l<=tree[root].left && tree[root].right<=r){
            tree[root].val+=(tree[root].right-tree[root].left+1)*add;
            tree[root].lazy+=add;
            return;
        }
        push_down(root);
        if(tree[root].val<0) tree[root].val=0;
        int mid=(tree[root].right+tree[root].left)>>1;
        if(r<=mid) updata(root<<1,l,r,add);
        if(l>mid) updata(root<<1|1,l,r,add);
        tree[root].val=tree[root<<1].val+tree[root<<1|1].val;
    }
    void MakeTree(int x,int l,int r)
    {
        tree[x].left=l;
        tree[x].right=r;
        if(l==r)
        {
            tree[x].val=1;
        }
        else
        {
            int mid=(l+r)/2;
            MakeTree(x*2,l,mid);
            MakeTree(x*2+1,mid+1,r);
            tree[x].val=tree[x*2].val+tree[x*2+1].val;
        }
    }
    
    long long query(int root,int l,int r){
        if(l<=tree[root].left && tree[root].right<=r){
            return tree[root].val;
        }
        push_down(root);
        int mid=(tree[root].right+tree[root].left)>>1;
        if(r<=mid) return(query(root<<1,l,r));
        if(l>mid) return(query(root<<1|1,l,r));
    }
    
    int main(void){
        
        scanf("%d%d",&n,&m);
        MakeTree(1,0,n);
        For(i,1,m){
            int a,b;
            scanf("%d%d",&a,&b);
            updata(1,a,b,-1);
        }
        printf("%lld",query(1,0,n));
        return 0;
    }
    
  • @ 2018-10-16 20:01:51
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define N 10000001
    using namespace std;
        
        
    int l,m,cnt,flag[N]={0};
    
    struct zzf{
        int left,right,tot;
        int mid;
        long long tree,tag;
    }zzf_[N];
        
        
    void maketree(int bb,int ll,int n){
        zzf_[n].left=bb;
        zzf_[n].right=ll;
        zzf_[n].tot=ll-bb+1;
        if(bb==ll){
            zzf_[n].tree=1;
        }
        else{
            int mid=(bb+ll)/2;
            zzf_[n].mid=mid;
            maketree(bb,mid,n*2);
            maketree(mid+1,ll,n*2+1);   
            zzf_[n].tree=zzf_[n*2].tree+zzf_[n*2+1].tree;
        }
        
        
    }   
        
        
    void addnode(int v,long long z){
        zzf_[v].tree+=z*zzf_[v].tot;
        if(zzf_[v].tree<=0) 
        {
            zzf_[v].tree=0;
            flag[v]++;  
        }
        if(flag[v]==1) {
            cnt++;
            flag[v]=-10;
        }
        zzf_[v].tag+=z;
    }   
        
        
    void clear(int v){
        addnode(v*2,zzf_[v].tag);
        addnode(v*2+1,zzf_[v].tag);
        zzf_[v].tag=0;
    }   
    
    
    void add(int v,int x,int y,int z){
        if(zzf_[v].left==x && zzf_[v].right==y)
        {
            addnode(v,z);
            return;
        }
        clear(v);
        if(y<=zzf_[v].mid)
            add(v*2,x,y,z);
        else if(x>zzf_[v].mid)
            add(v*2+1,x,y,z);
        else {
            add(v*2,x,zzf_[v].mid,z);    
            add(v*2+1,zzf_[v].mid+1,y,z);
        }       
        zzf_[v].tree=zzf_[v*2].tree+zzf_[v*2+1].tree;
        if(zzf_[v].tree<0) zzf_[v].tree=0;
    }   
    
    
    long long find(int v,int x,int y){
        if(zzf_[v].left==x && zzf_[v].right==y)
            return zzf_[v].tree;
        clear(v);
        if(y<=zzf_[v].mid)
            return find(v*2,x,y);
        if(x>zzf_[v].mid)
            return find(v*2+1,x,y);
        return find(v*2,x,zzf_[v].mid)+find(v*2+1,zzf_[v].mid+1,y);         
    }
    
    
    
    
    int main(void){
    //    freopen("tree.in","r",stdin);
    //    freopen("tree.out","w",stdout);
    
        scanf("%d%d",&l,&m);
        maketree(0,l,1);
     
        For(i,1,m){
            int la;
            scanf("%d",&la);
            if(la==0){
                int x,y;
                scanf("%d%d",&x,&y);
                add(1,x,y,-1);
            }
            else{
                int x,y;
                scanf("%d%d",&x,&y);
                add(1,x,y,1);
              }
          }
        printf("%d\n%d",cnt,find(1,0,l));
        
    //    fclose(stdin);
    //    fclose(stdout);
        return 0;
    }   
        
    
  • @ 2018-10-16 19:47:32

    线段树模板

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define N 1000001
    using namespace std;
    
    struct zzf{
        int l,r,tot;
        int mid;
        long long val,tag;  
    }tree[N];
    int n,m,cnt;
    long long T[N],A[N];
    
    void addnode(int node,long long z){
        tree[node].val+=z*tree[node].tot;
        tree[node].tag+=z;
    }
    
    void clear(int node){
        addnode(node*2,tree[node].tag);
        addnode(node*2+1,tree[node].tag);
        tree[node].tag=0;
    }
    
    void build(int node,int l,int r){
        tree[node].l=l;
        tree[node].r=r;
        tree[node].tot=r-l+1;
        if(l==r){
            tree[node].val=T[l];
            return;
        }
        else{
            int mid=(l+r)/2;
            tree[node].mid=mid;
            build(node*2,l,mid);    
            build(node*2+1,mid+1,r);
            tree[node].val=tree[node*2].val+tree[node*2+1].val;
        }
    }
    
    void add(int node,int l,int r,int num){
        if(tree[node].l==l && tree[node].r==r)
        {
            addnode(node,num);
            return;
        }
        clear(node);
        if(r<=tree[node].mid)
            add(node*2,l,r,num);
        else if(l>tree[node].mid)
            add(node*2+1,l,r,num);
        else {
            add(node*2,l,tree[node].mid,num);    
            add(node*2+1,tree[node].mid+1,r,num);
        }       
        tree[node].val=tree[node*2].val+tree[node*2+1].val;
        if(tree[node].val<0) tree[node].val=0;    
    }
    
    long long find(int node,int x,int y){
       
        if(tree[node].l==x && tree[node].r==y)
            return tree[node].val;
         clear(node);
        if(y<=tree[node].mid)
            return find(node*2,x,y);
        if(x>tree[node].mid)
            return find(node*2+1,x,y);
        return find(node*2,x,tree[node].mid)+find(node*2+1,tree[node].mid+1,y);     
        
    }
    
    int main(void){
    //    freopen("tree.in","r",stdin);
    //    freopen("tree.out","w",stdout);
          scanf("%d%d",&n,&m);
                
          For(i,1,n){
            scanf("%lld",&T[i]);
          }             
          
          build(1,1,n);
                     
          For(i,1,m){
            int la;
            scanf("%d",&la);
            if(la==1){
                int x,y,k;
                scanf("%d%d%d",&x,&y,&k);
                add(1,x,y,k);
            }
            else{
                int x,y;
                scanf("%d%d",&x,&y);
                cnt++;
                A[cnt]=find(1,x,y);
              }
          }
        For(i,1,cnt) printf("%lld\n",A[i]);
        
    
    //    fclose(stdin);
    //    fclose(stdout);
        return 0;
    }
    
  • @ 2018-09-21 19:56:05
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define Dor(i,r,l) for(int i=r;i>=l;i--)
    #define inf 21
    using namespace std;
    struct zzf{
        int x[inf],y[inf],z[inf];
    }pos[inf];
    int t,n[21],h[21],r[21];
    int fa[inf];
    
    
    int getf(int x){
        if(x==fa[x]) return x;
        fa[x]=getf(fa[x]);
        return fa[x];
    }
    
    bool combine(int x,int y){
        x=getf(x);
        y=getf(y);
        fa[x]=y;
    }
    
    int main()
    {
        scanf("%d",&t);
        
        For(i,1,t){
            scanf("%d%d%d",&n[i],&h[i],&r[i]);
            
            For(j,1,n[i]){
                
                scanf("%d%d%d",pos[i].x[j],pos[i].y[j],pos[i].z[j]);
                
            }
            
            
        }
        
        
        return 0;
    }
    
  • @ 2018-09-21 18:11:04
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define Dor(i,r,l) for(int i=r;i>=l;i--)
    using namespace std;
    
    struct zzf{
        int num,dis,x,y,lev;
    }t[5000];
    
    int n,m,k,a[2100][2100],cnt=0,ans=0;
    
    bool cmp(zzf A,zzf B){
        return (A.num>B.num)?true:false; 
    }
    
    
    int main(void){
        
        scanf("%d%d%d",&n,&m,&k);
        
        For(i,1,n){
            For(j,1,m){
    
                scanf("%d",&a[i][j]);
                
                if(a[i][j]>0){
     //               printf("a[%d][%d]=%d\n",i,j,a[i][j]);
                    cnt++;
                    t[cnt].num=a[i][j];
                    t[cnt].x=i;
                    t[cnt].y=j;
                }
    
            }
        }
       // printf("%d\n",cnt);
        sort(t+1,t+cnt+1,cmp);
        
        t[1].dis=max(t[1].x,t[1].y);
        t[1].lev=max(t[1].x,t[1].y);
        if(t[1].dis+t[1].lev+1>k){
            ans=0;
        }
        else{
        k-=t[1].dis;
        ans+=t[1].num;
        For(i,2,cnt){
            t[i].dis=abs(t[i].x-t[i-1].x)+abs(t[i].y-t[i-1].y);
            t[i].lev=max(t[i].x,t[i].y); 
            if(t[i].dis+t[i].lev+1>=k) i=cnt+1;
            else{
                k-=t[i].dis+1;
                ans+=t[i].num;
            }
        }
    
    }
    
        printf("%d\n",ans);
        return 0;
    }
    
    
  • @ 2018-09-20 20:01:00
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define Dor(i,r,l) for(int i=r;i>=l;i--)
    using namespace std;
    
    struct zzf{
        int num,dis,x,y;
    }t[50];
    
    int n,m,k,a[21][21],cnt=0,ans=0;
    
    bool cmp(zzf A,zzf B){
        return (A.num>B.num)?true:false; 
    }
    
    int find(int pos,int kk,int ans1){
        
    }
    
    int main(void){
        
        scanf("%d%d%d",&n,&m,&k);
        
        For(i,1,n){
            For(j,1,m){
    
                scanf("%d",&a[i][j]);
                
                if(a[i][j]>0){
                    printf("a[%d][%d]=%d\n",i,j,a[i][j]);
                    cnt++;
                    t[cnt].num=a[i][j];
                    t[cnt].x=i;
                    t[cnt].y=j;
                }
    
            }
        }
        printf("%d\n",cnt);
        sort(t+1,t+cnt+1,cmp);
        
        t[1].dis=max(t[1].x,t[1].y);
    
        For(i,2,cnt){
            t[i].dis=abs(t[i].x-t[i-1].x)+abs(t[i].y-t[i-1].y);
        }
        
        For(i,1,cnt) printf("t%d=%d %d dis=%d\n",i,t[i].x,t[i].y,t[i].dis);
        
        int pos=1;
        ans=find(pos,k,0);
    
        printf("\n%d",ans);
        return 0;
    }
    
  • @ 2018-09-19 19:00:24
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #define inf 101
    #define For(i,l,r) for(int i=l;i<=r;i++)
    #define Dor(i,r,l) for(int i=r;i>=l;i--)
    using namespace std;
    
    struct zzf{
        int sum;    
    };
    zzf T[inf];
    int t,n,minn[24]={0};
    
    int boom(zzf a[inf],int i,int cnt,int ans,int pos){//2
        if(a[i]==4){
            
        }
    }
    
    int dshunzi(zzf a[inf]){//
        
    }
    
    
    int dfs(zzf a[inf],int cnt,int ans,int pos){
        if(cnt==0) return 0;
        zzf b[inf];
    
        For(i,1,11){
            switch (i){
                case 1 : {
                    For(i,0,13) b[i].sum=a[i].sum;
                    int k=b[0].sum;
                    if(b[0].sum){
                        b[0].sum=0;
                        ans++;
                    }
                    dfs(b,cnt-k,ans,pos);
                    break;
                }
                case 2 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    For(i,1,13) boom(b,i);
                    break;
                }
                case 3 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 4 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 5 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 6 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 7 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 8 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 9 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 10 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
                case 11 :{
                    For(i,0,13) b[i].sum=a[i].sum;
                    break;
                }
            }
            if(minn[pos]>ans) minn[pos];
        }
    
        return 0;
    }
    
    void init(){
        For(i,1,t){
            For(i,1,n){
                int a,b;
                scanf("%d%d",&a,&b);
                T[a].sum++;
            }
        }
    }
    
    
    int main(void){
        scanf("%d%d",&t,&n);    
        
        init();
    
        For(i,1,t) minn[i]=dfs(T,n,0,i);
        
        return 0;
    }
    
    
  • 1