AtCoder Beginner Contest 203 A-E

随便更更。。

A:

题意:输入三个数a、b、c,如果有两数相同输出第三个数;否则输出0。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <cstring>
 9 using namespace std;
10 typedef pair<int,int> P;
11 typedef long long ll;
12 const int INF=0x7fffffff;
13 const int eps=1e-7;
14 const long long mod=1e9+7;
15 int main(void)
16 {
17   int a,b,c;
18   cin>>a>>b>>c;
19   int x=a^b^c;
20   if(x==a||x==b||x==c)cout<<x<<endl;
21   else cout<<0<<endl;
22   return 0;
23 }

B:

题意:求i0j的和,其中1 <= i <= N ,1 <= j <= K。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <cstring>
 9 using namespace std;
10 typedef pair<int,int> P;
11 typedef long long ll;
12 const int INF=0x7fffffff;
13 const int eps=1e-7;
14 const long long mod=1e9+7;
15 long long sum,n,k;
16 int main(void)
17 {
18     cin>>n>>k;
19     for(int i=1;i<=n;++i)
20     {
21         for(int j=1;j<=k;++j)
22         {
23             sum+=i*100+j;
24         }
25     }
26     cout<<sum<<endl;
27     return 0;
28 }

C:

题意:

从下标0出发,每前进一步需要1元,刚开始有K元,且有N个朋友,第i个朋友在下标ai,可以给你bi元,求能到达的最远下标。

分析:排序后对朋友顺序遍历即可。(刚开始还以为数据是升序的。。)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <cstring>
 9 using namespace std;
10 typedef pair<int,int> P;
11 typedef long long ll;
12 const int INF=0x7fffffff;
13 const int eps=1e-7;
14 const long long mod=1e9+7;
15 const int maxn=210000;
16 long long n,k,r=1;
17 struct fd
18 {
19     long long a,b;
20 }f[maxn];
21 bool cmp(fd p,fd q)
22 {
23     return p.a<q.a;
24 }
25 int main(void)
26 {
27     cin>>n>>k;
28     for(int i=1;i<=n;++i)cin>>f[i].a>>f[i].b;
29     sort(f+1,f+1+n,cmp);
30     while(r<=n&&k>=f[r].a-f[r-1].a)k=k-f[r].a+f[r-1].a+f[r++].b;
31     cout<<f[r-1].a+k<<endl;
32     return 0;
33 }

D:

题意:给定一个N*N的矩阵,每个位置上有一个元素,现求其中一个K*K的矩阵(K<=N),使得矩阵所有元素中的中位数最小,输出最小的中位数。

分析:看到最小最大容易想到二分,看到矩阵容易想到前缀和。假设当前的最小中位数为x,我们利用前缀和统计在一个K*K矩阵中有多少个元素是不大于x的。假设当前位置的元素大于x,那么该位置的前缀和减一;否则加一,即前缀和方程为pre[i][j]=pre[i][j-1]+pre[i-1][j]-pre[i-1][j-1]+(a[i][j]<=x?1:-1)。然后枚举右下角的坐标(i,j),判断是否存在一个pre[i][j],满足pre[i][j]-pre[i-k][j]-pre[i][j-k]+pre[i-k][j-k]<=0,如果有说明x成立,二分向左移动;否则二分向右移动。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <cstring>
 9 using namespace std;
10 typedef pair<int,int> P;
11 typedef long long ll;
12 const int INF=0x7fffffff;
13 const int eps=1e-7;
14 const long long mod=1e9+7;
15 int n,k,a[810][810],pre[810][810];
16 bool check(int mid)
17 {
18     for(int i=1;i<=n;++i)
19     {
20         for(int j=1;j<=n;++j)
21         {
22             pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
23             if(a[i][j]>mid)pre[i][j]++;
24             else pre[i][j]--;
25         }
26     }
27     for(int i=k;i<=n;++i)
28     {
29         for(int j=k;j<=n;++j)
30         {
31             if(pre[i][j]-pre[i-k][j]-pre[i][j-k]+pre[i-k][j-k]<=0)return true;
32         }
33     }
34     return false;
35 }
36 int main(void)
37 {
38     cin>>n>>k;
39     for(int i=1;i<=n;++i)
40     {
41         for(int j=1;j<=n;++j)cin>>a[i][j];
42     }
43     int l=0,r=1e9;
44     while(l<r)
45     {
46         int mid=(l+r)/2;
47         if(check(mid))r=mid;
48         else l=mid+1;
49     }
50     cout<<r<<endl;
51     return 0;
52 }

E:

题意:行、列下标均为0到2N,刚开始白块在(0,N)的位置,有M个黑块,对于所在位置(i,j),在不越过边界的前提下白块可以移动到(i+1,j+1)或(i+1,j-1)的黑块上或者移动到(i+1,j)的非黑块上,求Y的数量,其中Y为白块到达下边界(2N,Y)的第二坐标。

分析:刚开始看错题浪费了大半时间,以为不能跳到黑块上。。。用一个set记录答案,设为ans,刚开始ans只有一个元素n。对于某一个黑块(x,y),它只会影响上面的三个位置(x-1,y-1),(x-1,y),(x-1,y+1),由于一个位置可能受到多个黑块影响,我们不妨一次考虑一行的黑块,记录所有被影响到的位置,不妨记录在set中,设为temp。再设一个pass记录当前行所能到达的位置,对于某一个被影响的位置(x,y),假设它是可到达的,如果要移动到下一行,那么要满足(x+1,y-1)或者(x+1,y+1)为黑块,或者(x+1,y-1)为非黑块,如果条件成立则将新的坐标加入到pass中。处理完一行的黑块后,从ans中去掉temp,并加入pass。最后的ans即为答案。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <cstring>
 9 using namespace std;
10 typedef pair<int,int> P;
11 typedef long long ll;
12 const int INF=0x7fffffff;
13 const int eps=1e-7;
14 const long long mod=1e9+7;
15 const int maxn=210000;
16 int n,m,x[maxn],y[maxn];
17 int main(void)
18 {
19     cin>>n>>m;
20     map<int,set<int>>mp;
21     for(int i=0;i<m;++i)cin>>x[i]>>y[i],mp[x[i]].insert(y[i]);
22     set<int>ans;
23     ans.insert(n);
24     for(auto it=mp.begin();it!=mp.end();++it)
25     {
26         set<int>temp;
27         for(auto pos:it->second)
28         {
29             temp.insert(pos-1);
30             temp.insert(pos+1);
31             temp.insert(pos);
32         }
33         set<int>pass;
34         for(auto pos:temp)
35         {
36             if(ans.count(pos-1)&&it->second.count(pos)||ans.count(pos)&&!it->second.count(pos)||ans.count(pos+1)&&it->second.count(pos))pass.insert(pos);
37         }
38         for(auto pos:temp)
39         {
40             if(ans.count(pos))ans.erase(pos);
41         }
42         for(auto pos:pass)
43         {
44             ans.insert(pos);
45         }
46     }
47     cout<<ans.size()<<endl;
48     return 0;
49 }

F:

待补。

终究独木难支。
原文地址:https://www.cnblogs.com/yanying7/p/14833771.html