标签:cin 多少 数据 操作 长度 order 题解 开始 pac
# A
题意:
给定a,b,通过对a的操作使得a,b相等,只能加奇数,减偶数,至少需要多少次操作
题解:
操作最多是2只需要判断奇偶即可,最多通过一次操作改变奇偶,再补上差值
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int t; 5 cin>>t; 6 while(t--){ 7 int a,b; 8 cin>>a>>b; 9 int ans=1; 10 if(a==b){ 11 cout<<"0"<<endl; 12 continue; 13 } 14 if(a>b){ 15 int x=a-b; 16 if(x%2){ 17 ans++; 18 } 19 } 20 else{ 21 int x=b-a; 22 if(!(x%2)){ 23 ans++; 24 } 25 } 26 cout<<ans<<endl; 27 } 28 }
# B
题意:
给定两个数组a和p,a中是无序的,只能通过交换p数组中所有的数字和他后面一项来改变顺序,问是否能达到正序
题解:
将p中的数字用unorder_map记录下来,pair记录a的下标和值,按值排序,从值最大的开始到他应该在的位置,所有数进行这样的操作,如果某一个数不可以进行就说明不能完成,因为哪一个数先移动都可以,所以只要看在原来的位置能不能移动到应该在的位置就可以
1 #include<bits/stdc++.h> 2 #define pii pair<int,int> 3 #define fi first 4 #define se second 5 using namespace std; 6 const int N=110; 7 pii a[N]; 8 int n,m; 9 void work(){ 10 cin>>n>>m; 11 unordered_map<int,bool>h; 12 for(int i=1;i<=n;i++){ 13 cin>>a[i].fi; 14 a[i].se=i; 15 } 16 for(int i=1;i<=m;i++){ 17 int x; 18 cin>>x; 19 h[x]=1; 20 } 21 sort(a+1,a+n+1); 22 bool ok=1; 23 for(int i=n;i>=1;i--){ 24 if(a[i].se != i ){ 25 for(int j = a[i].se;j<i;j++){ 26 if(!h[j]) { 27 ok=0; 28 break; 29 } 30 } 31 } 32 if(!ok) break; 33 } 34 35 if(ok) puts("YES"); 36 else puts("NO"); 37 } 38 int main(){ 39 int t; 40 cin>>t; 41 while(t--){ 42 work(); 43 } 44 }
# C
题意:
一个字符串s,长度n,一个数组p,长度m,每个p[i]都会访问s[1 ~ p[i] ],最后还会访问整个s,统计26个字母每个字母被访问的次数
题解:
数据很大,只能允许O(NlogN)的做法,将p排序,最少的p的值一共会进行m+1次,每次都递减即可,在p中加一个p[m+1]=n,每次只需要从上一个结尾后一个开始即可。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N=2e5+10; 4 string str; 5 int p[N]; 6 int n,m; 7 int cnt[26]; 8 void work(){ 9 cin>>n>>m>>str; 10 for(int i=1;i<=m;i++) 11 cin>>p[i]; 12 sort(p+1,p+m+1); 13 14 p[m+1]=n; 15 int now=m+1; 16 memset(cnt,0,sizeof cnt); 17 for(int i=1;i<=m+1;i++){ 18 if(p[i]>p[i-1]) 19 for(int j=p[i-1]+1;j<=p[i];j++) 20 cnt[str[j-1]-‘a‘]+=now; 21 now--; 22 } 23 for(int i=0;i<26;i++) 24 cout<<cnt[i]<<‘ ‘; 25 cout<<endl; 26 } 27 int main(){ 28 int t; 29 cin>>t; 30 while(t--){ 31 work(); 32 } 33 }
# D
题意:
给定三个数字a,b,c
满足a <= b <= c ,每次操作可以使任意数+1或-1
求最少的操作数使得 a | b , b | c
并且始终满足a<=b<=c
数据范围:
t组数据,t<100
a <= b <= c < 1e4
题解:
从a开始暴力枚举 , a最大是2a,因为a~2a中间一定存在着能被b整除的数字,最小是1,b最小和a相等最大是2b
c由b推出
每次求出当前的操作数,更新答案求最小值即可
1 #include <bits/stdc++.h> 2 using namespace std; 3 void solve(){ 4 int a,b,c; 5 cin>>a>>b>>c; 6 int ans = INT_MAX; 7 8 int A = -1,B = -1,C = -1; 9 10 for(int nowa = 1; nowa <= 2 * a; nowa++){ 11 for(int nowb = nowa; nowb <= 2*b; nowb += nowa){ 12 for(int i = 0; i <= 1; i++){ 13 int nowc = nowb * (c/nowb) + i * nowb; 14 int res = abs(a-nowa) + abs(b-nowb) + abs(c-nowc); 15 if(ans > res){ 16 ans = res; 17 A = nowa; 18 B = nowb; 19 C = nowc; 20 } 21 } 22 } 23 } 24 cout<<ans<<endl<<A<<‘ ‘<<B<<‘ ‘<<C<<endl; 25 } 26 int main(){ 27 int t; 28 cin>>t; 29 while(t--){ 30 solve(); 31 } 32 }
# E
题意:
题解:
# F
题意:
题解:
标签:cin 多少 数据 操作 长度 order 题解 开始 pac
原文地址:https://www.cnblogs.com/hhyx/p/12398396.html