CQUPT WEEKLY TRAINING (5)DIV2 解题报告
来源:互联网 发布:2016全国交通事故数据 编辑:程序博客网 时间:2024/06/07 20:05
A题:注意到n范围只有10^3,直接打个1000以内的由4和7构成的数字的数组就行了。
int a[13]={4,7,44,47,74,77,444,447,474,477,744,774,777};
B题:读好题意就简单了。在给定字符串中找子串,这个子串由4和7组成且出线次数最多,如果有多个子串出现次数相同,输出最小的。
考虑单个字符肯定是最小的字符串,那么答案如果存在,一定是“4”或者“7“。然后统计一下‘4’和‘7’的个数就KO了。
#include <cstdio>#include <algorithm>#include <iostream>#include <string>#include <cstring>using namespace std;int main(){ string s; int a,b; while(cin>>s) { a=0,b=0; for(int i = 0; i < s.size(); i++) { if(s[i] == '4') a++; if(s[i]=='7') b++; } if(a==0&&b==0) printf("-1\n"); else if(a>=b) printf("4\n"); else printf("7\n"); } return 0;}
C题:next(x)指最小的大于等于x的lucky number。现在给定l和r,求sigma(next[i])(l<=i<=r)。PS:sigma是什么不知道,请自行百度。
一眼看下去以为会用二分。但是l,r范围才10^9。范围内的lucky number数量也不多,搜索一下打个表,排序一下就行了。另外就是sum(x)中x落在哪个区间的细节处理问题上了。详情见code。
#include <cstdio>#include <algorithm>#include <iostream>#include <string>#include <cstring>#include <cmath>using namespace std;typedef long long LL;const int maxn = 10000;LL d[maxn];int tot = 0;void dfs(LL x,int cur){ d[tot++] = x; if(cur>10) return ; dfs(x*10+4,cur+1); dfs(x*10+7,cur+1);}LL sum(LL x){ if(x==0) return 0; LL sum = 0; for(int i=0;i<tot;i++) { if(x>=d[i]) sum+=d[i]*(d[i]-d[i-1]); else { sum+=d[i]*(x-d[i-1]); break; //这时候表示已经到枚举到最大的区间内 } } return sum;}int main(){ dfs(4,0); dfs(7,0); sort(d,d+tot); LL l,r; while(cin>>l>>r) { cout<<sum(r)-sum(l-1)<<endl; }}
D题:前面至少有a个人,后面至多有b个人。总共n个,问可以在的位置的数量。
有点点贪心的思想,n减去a后就得到剩下的位置可能性,然后再和b比较,满足b的限制。
#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <iostream>using namespace std;int main(){ int n,a,b,c; while(cin>>n>>a>>b) { c = n-a; if(c-1>b) { cout<<b+1<<endl; } else { cout<<c<<endl; } }}
E题:你输入n,m,n为数字的个数,m为数字的位数(数字可以出现前缀零)。然后要求你用一种规定来从新排列每一个数,比如3257按照(3,2,4,1)的规则就变成了5273.(要求每一个数都按照这种规定),其实规定就是数字的位数的全排列。最后要求输出变换后的数字组合中的最大值减去最小值的差值最小。找出这个最小差值。(n,m<=8)。
思路:枚举排列,维护最大最小值。注意这里用一个辅助数组。细节见代码。挺不错的一道题。
#include <iostream>#include <algorithm>#include <stdio.h>using namespace std;int main(){int n,k,i,j,min,max,num,ans=1000000000;int a[]={0,1,2,3,4,5,6,7};char s[8][10];scanf("%d%d",&n,&k);for (i=0; i<n; i++)scanf("%s",s[i]);do{min=1000000000;max=0;for (i=0; i<n; i++){num=0;for (j=0; j<k; j++)num=num*10+(s[i][a[j]]-'0');if (min>num) min=num;if (max<num) max=num;}if (max-min<ans) ans=max-min;}while(std::next_permutation(a,a+k));printf("%d\n",ans);return 0;}
0 0
- CQUPT WEEKLY TRAINING (5)DIV2 解题报告
- CQUPT WEEKLY TRAINING (2)DIV2 解题报告
- CQUPT WEEKLY TRAINING (4)DIV2 解题报告
- CQUPT WEEKLY TRAINING (6)DIV2 解题报告
- CQUPT WEEKLY TRAINING (7)DIV2 解题报告
- CQUPT WEEKLY TRAINING (1)解题报告
- CQUPT WEEKLY TRAINING (3)解题报告
- Mango Weekly Training Round #3 解题报告
- Mango Weekly Training Round #6 解题报告
- 2015 CQUPT WEEKLY TRAINING (1)题解和专题内容推荐
- LeetCode Weekly Contest 56 解题报告
- codeforces #133 div2 解题报告
- codeforces #134 div2 解题报告
- SRM 556 DIV2 解题报告
- codeforce 192 div2解题报告
- 130720CF div2解题报告
- [Codeforces]Round246 div2 解题报告
- codeforces#254DIV2解题报告
- Linux下如何打包文件
- 晶体放大电路的识图方法
- C运行时库分析-检测内存泄漏篇(模拟实现)
- 1003:A+B 输入输出练习IV【JAVA实现】
- (转载)DLL编写教程
- CQUPT WEEKLY TRAINING (5)DIV2 解题报告
- 场效应管放大电路的识图方法
- Android 中的拿来主义(编译,反编译,AXMLPrinter2,smali,baksmali)!
- c语言实现把‘0’和‘1’字符串转化为二进制压缩保存成二进制文件
- 总结下这一年来学到了什么
- excel 查找一列在另一列的位置
- HTTP POST GET 本质区别详解
- 查找文件夹下包含指定字符串的文件名字
- uva--714+二分(最大值最小化问题)