字符串模拟,暴力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 }
分情况大讨论下。
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 }
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