容斥原理——CodeForces 1017B

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AC_jie/article/details/81570537

今天来不昨天的题,这个题想到了思路,但是去重出了问题,一开始拿set去重,直接爆掉,GG。忽然发现自己真正会的东西真少,从今天开始就要脚踏实地的一步一步地将自己的基础打牢。
一会儿整理stl的用法。
先来说一下这个题,本质是一个容斥原理,整天给师弟们讲,自己还是不会用QAQ.

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef long long LL;
int main()
{
    int n;
    while(cin >> n)
    {
        string s1,s2;
        cin >> s1 >> s2;
        LL a = 0,b = 0,c = 0,d = 0;
        for(int i = 0;i < n;++i)
        {
            if(s1[i] == '1') a++;
            else b++;
            if(s2[i] == '0')
                if(s1[i] == '0') c++;
                else d++;
        }
        LL ans = 0;
        ans = (c * a + d * b) - (c * d);//容斥原理
        cout << ans << endl;
    }
    return 0;
}

codeforces 621B wrong answer 求解

02-03

题目rnrnB. Wet Shark and Bishopsrnrntime limit per test:2 secondsrnrnmemory limit per test:256 megabytesrnrninput:standard inputrnrnoutput:standard outputrnrnrnToday, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.rnrnWet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.rnrnrnInputrnrnThe first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.rnrnEach of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.rnrnrnOutputrnrnOutput one integer — the number of pairs of bishops which attack each other. rnrnrnSample test(s)rnrnrnrnInputrn5rn1 1rn1 5rn3 3rn5 1rn5 5rnrnrnrnOutputrn6rnrnrnrnInputrn3rn1 1rn2 3rn3 5rnrnrnrnOutputrn0rnrnrnrnNoternrnIn the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.rnrnrnrnrn我的代码rn#includern#includern#includern#includern#includern#includern#includernusing namespace std;rnint x[1050],y[1050],n,a[1050][1050];rnbool panduan(int i,int j)rnrn if((i>=1)&&(i<=1000)&&(j>=1)&&(j<=1000))rn return true;rn elsern return false;rnrnint main(void)rnrn long long sum=0;rn int ii,jj,status,j,i,temp_a,temp_b;rn scanf("%d",&n);rn for(i=1;i<=1000;i++)rn for(j=1;j<=1000;j++)rn a[i][j]=0;rn for(i=0;i

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试