您的位置:首页 > 其它

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

2016-07-23 10:53 465 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 水题 STL-SET