Educational Codeforces Round 49(A,B,C,D)

Palindromic Twist

字符串模拟,暴力check下。

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <deque>
 5 #include <stack>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15
16 #define eps 1e-8
17 #define PI acos(-1.0)
18 #define INF 0x3f3f3f3f
19 #define FAST_IO ios::sync_with_stdio(false)
20
21 typedef long long LL;
22
23 bool check(int a,int b){
24     for(int i=a-1;i<=a+1;i++){
25         if(i==a) continue;
26         for(int j=b-1;j<=b+1;j++){
27             if(j==b) continue;
28             if(i==j) return true;
29         }
30     }
31     return false;
32 }
33
34 int main(){
35     FAST_IO;
36     int t;
37     cin>>t;
38     while(t--){
39         string s;
40         int n,f=0;
41         cin>>n>>s;
42         for(int i=0,j=n-1;i<j;i++,j--){
43             if(s[i]!=s[j]){
44                 if(check(s[i],s[j])) f=f;
45                 else {f=1;break;}
46             }
47         }
48         if(f==1) cout<<"NO"<<endl;
49         else cout<<"YES"<<endl;
50     }
51     return 0;
52 }

Numbers on the Chessboard

分情况大讨论下。

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <deque>
 5 #include <stack>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15
16 #define eps 1e-8
17 #define PI acos(-1.0)
18 #define INF 0x3f3f3f3f
19 #define FAST_IO ios::sync_with_stdio(false)
20
21 typedef long long LL;
22
23 int main(){
24     FAST_IO;
25     LL n,q;
26     cin>>n>>q;
27     for(LL i=1;i<=q;i++){
28         LL x,y;
29         cin>>x>>y;
30         if((x+y)%2==0){
31             if(n%2==0){
32                 cout<<(x-1)*n/2+(y+1)/2<<endl;
33             }
34             else{
35                 if((x-1)%2==0){
36                     cout<<(x-1)/2*n+(y+1)/2<<endl;
37                 }
38                 else{
39                     cout<<x/2*n-(n/2-(y+1)/2)<<endl;
40                 }
41             }
42         }
43         else{
44             if(n%2==0){
45                 cout<<n*n/2+(x-1)*n/2+(y+1)/2<<endl;
46             }
47             else{
48                 if((x-1)%2==0){
49                     cout<<n*n/2+1+(x-1)/2*n+(y+1)/2<<endl;
50                 }
51                 else{
52                     cout<<n*n/2+1+x/2*n-(n/2+1-(y+1)/2)<<endl;
53                 }
54             }
55         }
56     }
57     return 0;
58 }

Minimum Value Rectangle

Mouse Hunt

tarjan强连通分量,先缩点,在缩完的每个点中拿出度为0的点的最小的值。

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <deque>
 5 #include <stack>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15
16 #define eps 1e-8
17 #define PI acos(-1.0)
18 #define INF 0x3f3f3f3f
19 #define FAST_IO ios::sync_with_stdio(false)
20
21 typedef long long LL;
22 const int N=2e5+10;
23 int scc,idx;
24 int dfn[N],low[N],ins[N],col[N],val[N],c[N],out[N];
25 stack <int> sk;
26 vector <int> g[N],dag[N];
27
28 void tarjan(int u){
29     dfn[u]=low[u]=++idx;
30     sk.push(u);
31     ins[u]=1;
32     int v;
33     for(int i=0;i<g[u].size();i++){
34         v=g[u][i];
35         if(!dfn[v]) tarjan(v);
36         if(ins[v]) low[u]=min(low[u],low[v]);
37     }
38     if(dfn[u]==low[u]){
39         scc++;
40         int mn=INF;
41         do{
42             v=sk.top();
43             sk.pop();
44             ins[v]=0;
45             col[v]=scc;
46             mn=min(mn,c[v]);
47         }while(u!=v);
48         val[scc]=mn;
49     }
50 }
51
52 int main(){
53     int n;
54     scanf("%d",&n);
55     for(int i=1;i<=n;i++) scanf("%d",&c[i]);
56     for(int i=1;i<=n;i++){
57         int cur;
58         scanf("%d",&cur);
59         g[i].push_back(cur);
60     }
61     for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
62     for(int i=1;i<=n;i++){
63         int u=col[i];
64         for(int j=0;j<g[i].size();j++){
65             int v=col[g[i][j]];
66             if(u!=v){
67                 dag[u].push_back(v);
68                 out[u]++;
69             }
70         }
71     }
72     int ans=0;
73     for(int i=1;i<=scc;i++){
74         if(out[i]==0) ans+=val[i];
75     }
76     printf("%d\n",ans);
77     return 0;
78 }

原文地址:https://www.cnblogs.com/ehanla/p/9527419.html

时间: 2024-10-09 18:22:04

Educational Codeforces Round 49(A,B,C,D)的相关文章

Educational Codeforces Round 52F(树形DP,vector)

#include<bits/stdc++.h>using namespace std;int n,k;vector<int>son[1000007];int dp[1000007],depth[1000007],ans[1000007];//dp[i]表示离i最近的叶子节点距离i的深度,depth[i]表示以i为根,回到i所能到达的叶子节点的数量,ans[i]表示以i为根,能到达的叶子节点数目最大,即题意所需void dfs(int now){    if(!son[now].si

Educational Codeforces Round 26 (A B C)

A. Text Volume You are given a text of single-space separated words, consisting of small and capital Latin letters. Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text. Calculate

Educational Codeforces Round 49 (Rated for Div. 2)

C - Minimum Value Rectangle 题意:给n根木棒,选4根组成长方形,使得这个长方形的周长的平方比上其面积最小. 题解:对那个式子求导,发现对于同一个长来说,是长和宽越接近,上式越小.那么排序之后每个和他附近的一个组装一下就行了. map<int, int> m; vector<int> v; void test_case() { int n; scanf("%d", &n); m.clear(); for(int i = 1; i

Educational Codeforces Round 8(D. Magic Numbers(数位DP))

题目链接:点击打开链接 题意:给一个m一个d, 一个字符串a和b, 问在[a,b]范围内, 有多少个可以整除m的魔法数, 魔法数的定义是, 偶数位上都是d, 奇数位上都不是d. 思路:据说是典型的数位DP, 以前没做过数位DP, 感觉和DP差不多? 用d[i][j][p]表示当前到了第i位, 余数为j, p == 1表示目前和b串相等, p == 0表示已经比b串小了.  每次枚举第i位上放的数, 转移就行了. 区间也好弄, 我们可以先处理出小于等于b的所有情况ans1, 小于等于a的所有情况a

Educational Codeforces Round 49 (Rated for Div. 2) ABCD

A. Palindromic Twist You are given a string ss consisting of nn lowercase Latin letters. nn is even. For each position ii (1≤i≤n1≤i≤n) in string ss you are required to change the letter on this position either to the previous letter in alphabetic ord

Educational Codeforces Round 63-D(基础DP)

题目链接:https://codeforces.com/contest/1155/problem/D 题意:给定n个数,可以选择一段连续子段将其乘x,也可以不操作,求最大连续子段和. 思路:比赛时觉得是dp,但怎么也想不出来QAQ,dp太难了...赛后看了别人题解,找到状态和转移方程就很简单了,然而比赛时我就是想不到... 考虑下标i:有3种情况,可能[0,i]都没有乘x,可能i乘了x,可能[i,n]都不会乘x.分别用dp[i][0]表示以i结尾的最长子段和且 [0,i]都没乘x,dp[i][1

Educational Codeforces Round 8(E. Zbazi in Zeydabad(树状数组优化))

题目链接:点击打开链接 题意:一个n*m矩阵, 里面的格子除了'z'就是'.',问有多少个z形图案. 思路:因为n和m很大, 即使n^3复杂度也会超时.  如果按照最朴素的方法, 我们可以处理一下前缀和, 处理出一个格子向左l[i][j].向右r[i][j].斜向左下lr[i][j]连着的z到哪里为止, 这样我们用n^2复杂度枚举每一个格子作为z形图案的右上角,取min(l[i][j], lr[i][j]), 就可以立刻知道这个z形的最左下角到哪里, 然后在这个对角线上扫一遍, 看看向右最远是不

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

Educational Codeforces Round 58 (Rated for Div. 2)(待更新)

get人生第七场CF! 成绩:(exACM) rank AC3/7 Penalty104 rating() 题目:Educational Codeforces Round 58 (Rated for Div. 2) 错题题解: C. Division and Union 原文地址:https://www.cnblogs.com/xht37/p/10260260.html