Codeforces Round #560 (Div. 3)
题目大意:给定一个十进制的01串,使得他对10^x取模后结果等于10^y.
思路直接枚举后x位数字,当前仅当倒数第y+1个位置数字为1,其余位置为0时等式成立,从后往前依次遍历即可。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 using namespace std; 11 typedef long long LL; 12 const int maxn = 1e5+10; 13 LL n,x,y; 14 string str; 15 int main() 16 { 17 while(cin>>n>>x>>y){ 18 int ans = 0,tmp=0; 19 cin>>str; 20 reverse(str.begin(),str.end()); 21 for(int i=0;i<x;i++){ 22 if(i==y&&str[i]!='1')ans++; 23 else if(i!=y&&str[i]=='1')ans++; 24 } 25 cout<<ans<<endl; 26 } 27 return 0; 28 }
题目大意:给定n个问题,想问你最多能选取多少个问题并且满足 第k个问题数 a[k]>=k
思路:排序+贪心
从小到大遍历,若当前选取第x个数,若满足a[x]>=x,就选取当前的数,否则就跳过。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 using namespace std; 11 typedef long long LL; 12 const int maxn = 1e5+10; 13 LL n; 14 priority_queue<int,vector<int>,greater<int> >Q; 15 int main() 16 { 17 while(cin>>n){ 18 for(int x,i=1;i<=n;i++){ 19 cin>>x; 20 Q.push(x); 21 } 22 int k=1,ans=0; 23 while(Q.size()){ 24 if(Q.top()>=k)ans++,k++; 25 Q.pop(); 26 } 27 cout<<ans<<endl; 28 } 29 return 0; 30 }
题目大意:给定一个长度为N的字符串,求最少删除多少个字符使得剩下的字符串满足(a[i]!=a[i+1])(i= 1,3,5,7....) 最后输出最少删除的字符个数,已经删除后的字符串。
思路:模拟
从第一个位置往后计算,若满足a[i]!=a[i+1],则将这两个字符添加到目标字符串中,否则删除第i个字符,再从第i+1个字符往后遍历。最后注意特判一下(若n-删除的字符数 = 奇数,则要加上原字符串的最后一个)
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 using namespace std; 11 typedef long long LL; 12 const int maxn = 1e5+10; 13 LL n; 14 string str; 15 int main() 16 { 17 while(cin>>n){ 18 cin>>str; 19 string tmp=""; 20 int num=0; 21 for(int i=0;i<n-1;i++){ 22 if(str[i]!=str[i+1])tmp+=str[i],tmp+=str[i+1],i++; 23 else num++; 24 } 25 if((n-num)%2)tmp+=str[n-1]; 26 if(tmp.size()%2)tmp=tmp.substr(0,tmp.size()-1),num++; 27 cout<<num<<endl<<tmp<<endl; 28 } 29 return 0; 30 }
题目大意:给定一个长度为n的整数序列a,求一个最小的数X,使得X的所有因子(1,X除外)都在a序列中。若能找到则输出这个数,若不能找到则输出-1
思路:排序+思维
若一个数除1和他本身外所有的因子都在a序列中,则这个数等于min(a)*max(a);
所以我们只需要将a序列排序,从两端往中间判断 每一次的a[l]*a[r]是否相等,若存在不相等则输出-1,若全相同,则判断这个数的所有因子数-2是否等于N,等于N则输出这个数,否则输出-1 (若n==1,只需要判断当前数是否为质数,若为质数则输出该数的平方,否则输出-1)
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 using namespace std; 11 typedef long long LL; 12 const int maxn = 1e5+10; 13 const LL INF = 1e16; 14 LL n,t,a[400]; 15 string str; 16 LL count(LL n){ 17 LL s=1; 18 for(LL i=2;i*i<=n;i++){ 19 if(n%i==0){ 20 LL a=0; 21 while(n%i==0){ 22 n/=i; 23 a++; 24 } 25 s=s*(a+1); 26 } 27 } 28 if(n>1) s=s*2; 29 return s; 30 } 31 int main() 32 { 33 cin>>t; 34 while(t--){ 35 cin>>n; 36 for(int i=1;i<=n;i++)cin>>a[i]; 37 sort(a+1,a+n+1); 38 LL ans = a[1]*a[n]; 39 if(n==1)ans = a[1]*a[1]; 40 else{ 41 LL l=2,r=n-1; 42 while(l<=r){ 43 if(l==r){ 44 if(ans!=a[l]*a[r])ans=-1; 45 break; 46 } 47 if(a[l]*a[r]!=ans){ 48 ans = -1; 49 break; 50 }else l++,r--; 51 } 52 } 53 if(ans!=-1){ 54 LL num = count(ans)-2; 55 if(num!=n)ans=-1; 56 } 57 cout<<ans<<endl; 58 } 59 return 0; 60 }
题目大意:给定两个长度为n的序列a,b ,只能重新排列b数组的元素使得等式值最小。
思路:排序+贪心
有题意我们知道,a序列不能打乱顺序。对于a序列来说第i个位置的数再计算f(l,r)时,会被计算i*(n-i+1)次,所以我们将a序列的每一个位置都乘上 (i*(n-i+1)) ;
现在的问题转化为,重新排列a,b序列,使得他们对应位置乘积之和最小,结果对998244353取模,现在只需要用a的最小值乘上b的最大值,a的较小值乘上b的较大值..依次类推即可。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 using namespace std; 11 typedef long long LL; 12 const LL mod = 998244353; 13 const int maxn = 2e5+10; 14 typedef pair<LL,LL>P; 15 LL n,b[maxn],a[maxn]; 16 int main() 17 { 18 while(cin>>n){ 19 for(LL i=1;i<=n;i++){ 20 cin>>a[i]; 21 a[i] = (n-i+1)*i*a[i]; 22 } 23 for(int i=1;i<=n;i++)cin>>b[i]; 24 sort(a+1,a+n+1); 25 sort(b+1,b+n+1); 26 LL ans = 0; 27 for(int i=1;i<=n;i++) 28 ans = (ans + (a[i]%mod*b[n-i+1]%mod)%mod)%mod; 29 cout<<ans<<endl; 30 } 31 return 0; 32 }