Codeforces Round #364(Div. 2) A. Cards【模拟】 && B. Cells Not Under Attack【SET/规律】

Codeforces Round #364(Div. 2) A. Cards【模拟】 && B. Cells Not Under Attack【SET/规律】


A. Cards【模拟】


题目链接:
http://codeforces.com/contest/701/problem/A

题意:
每个人【n/2个人】拿两张牌且拿到的牌的和为平均数【sum/人数】,结果按顺序输出每个人的牌的情况

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 110;
int n, m, k;
int a[maxn],vis[maxn];
int main(){
    while(~scanf("%d",&n)){
        int sum = 0;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            sum += a[i];
        }
        sum/=(n/2);//average
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(a[i]+a[j]==sum&&!vis[i]&&!vis[j]){
                    if(a[i]<=a[j]) printf("%d %d\n",i,j);
                    else printf("%d %d\n",j,i);
                    vis[i]=vis[j]=1;//标记
                    break;
                }
            }
        }
    }
    return 0;
}

B. Cells Not Under Attack【规律】


题目链接:
http://codeforces.com/contest/701/problem/B

题意:
在n*n的格子棋盘里要放m个步操作:在某点( r , c )放一个车输出剩下可以摆旗子的格子数目。

题解:
初值sum为n^2个空的格子可放旗子,每放个车【此行此列都不能放旗子】,vis[1e+5][1e+5]开不了这么大;
法一:
简单的容器set,自动排除重复

#include <iostream>
#include <set>
using namespace std;
typedef long long LL;
LL n,m,x,y;
set<LL>a,b;
int main(){
    while(cin>>n>>m){
        for(int i=1;i<=m;i++){
            cin>>x>>y;
            a.insert(x);b.insert(y);
            cout<<(n-a.size())*(n-b.size())<<(i==m?"\n":" ");
        }
    }

    return 0;
}

法二:

想想规律如图,可知第一次放sum-2*n+1【排除重叠】,
第二次放则根据 这点( r , c )的行列来判断。
即设初始化,余下来的行列数nr=nc=n:
1、第r行第c列都没遍历过,排除重叠情况【sum–】,余下来的行数nr-=1、 列数 nc-=1
2、第c列没遍历过,第r行访问过,即第r行重叠了此时只有算列的,余下列数nc-=1
3、第r行没遍历过,第c列访问过,即第c列重叠了此时只有算行的余下行数nr-=1
每放一次车,sum只要减掉余下的行或列或行列
这里写图片描述

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 100100;
LL n,m,sum,r,c,nr,nc;
int visr[maxn];
int visc[maxn];
int main(){
    while(~scanf("%lld %lld",&n,&m)){
        memset(visr,0,sizeof(visr));
        memset(visc,0,sizeof(visc));
        sum = n*n;//9
        nr = nc = n;
        for(int i=1;i<=m;i++){
            scanf("%lld %lld",&r,&c);
            if(!visr[r]&&!visc[c]){//第r行第c列 没遍历过
                sum--;//排除重叠
                nr--;
                sum-=nr;
                visr[r] = 1;
                nc--;
                sum-=nc;
                visc[c] = 1;
            }
            if(visr[r] && !visc[c]){//第c列没遍历过,第r行访问过
                nc--;
                sum-=nr;
                visc[c] = 1;
            }
            if(visc[c] && !visr[r]){//第r行没遍历过,第c列访问过
                nr--;
                sum-=nc;
                visr[r] = 1;
            }
            if(sum>=0){
                printf("%lld",sum);
                printf("%c",i==m?'\n':' ');
            }
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值