希望每次打完cf都会有收获,希望自己对数据的处理和编码的能力能有提高。
以前都是无所谓的心态,犯过的错误可以一犯再犯,现在开始把解题的过程和反思记录在博客中,希望能够有所长进。
A
t :1e3; n:50
数据量很小,
思路:做的时候直接暴力判了重复;其实用数组标记一下重复更加简洁
#include <iostream>
#include<vector>
#include<string.h>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
using namespace std;
vector<char>v;
char aph[55];
int mark[30];
int main()
{
int t;
cin >> t;
while (t--) {
memset(mark,0,sizeof(mark));
int n;
cin >> n;
int flag = 1;
for (int i = 0; i < n; i++)
cin >> aph[i];
mark[aph[0]-'A'] = 1;
for (int i = 1; i < n; i++) {
if (aph[i] != aph[i - 1]) {
if (mark[aph[i] - 'A']) {
flag = 0;
break;
}
else
mark[aph[i] - 'A']=1;
}
}
if (flag == 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
注:memset()函数包含在string.h 头文件中
B
简单暴力,做的时候思路一定要清晰(耽误了很长时间…)
核心:找到同位数的潜在最大odinary即可,
eg: 239388 看222222 √;201001 看222222×
(并非去比较最高位和各位所出现的最小数字——直达核心判断标准222222行还是不行!)
更:其实是可以比较最高位和各位所出现的最小数字的,从次高位开始逐位判断有无大于最高位的数;有大于则直接break;有小于-1后break。
(代码来自——JNU_dh_AK_WF)
int solve(string n){
int res=0;
int sz=n.length();
res+=9*(sz-1);
res+=n[0]-'0';
bool flag=true;
for(int i=1;i<sz;i++){
if(n[i]>n[0]) break;
if(n[i]<n[0]){
res--;
break;
}
}
return res;
}
#include <iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int ans = 0;
int i = 0;
int nums[15];
int minn = 11;
if (n <= 9) {
cout << n << endl;
}
else {
int mark1 = n;
while (n) {
int tmp = n % 10;
nums[i] = tmp;
i++;
n = n / 10;
minn = min(minn, tmp);
}
ans += (i - 1) * 9;
int mark2 = nums[i - 1];
for (int k = 0; k < i - 1; k++)
mark2 = mark2 * 10 + nums[i - 1];
if (mark2 <= mark1)
ans += nums[i - 1];
else
ans += (nums[i - 1] - 1);
cout << ans << endl;
}
}
return 0;
}
纠错困难原因:
1.写代码之前思路就不明确;或者有想当然的情况(明显错误)。
2.在打表全部输出时,很快发现异常错误后,没有认真地理清楚思路,再次提出错误方案;心态逐渐失衡。
总结:上手写代码前,算法的思路一定是清晰的;简单问题可以打表来查看异常数据;改错时一定要思路清晰,放平心态!
注:本题的输入当做字符串来看,会节省一定的步骤(参看tls代码)
C
简单构造数据题
奇数偶数分开放置即可
(打rating的时候静下心来好好看也不会掉这么多分,5min就能搞定的题…)
#include <iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int nums[10005];
int main()
{
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n == 1)
cout << 1 << endl;
else if (n == 2)
cout << -1 << endl;
else {
int k = 0;
for (int i = 1; i <= n*n; ) {
nums[k] = i;
i += 2;
k++;
}
for (int i = 2; i <= n*n; ) {
nums[k] = i;
i += 2;
k++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << nums[i * n + j] << " ";
}
cout << endl;
}
}
}
return 0;
}
D
数学推导,小思维题
#include <iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<stack>
using namespace std;
#define maxn 200005
int nums[maxn];
//x1-x2 = y1-y2 => x1-y1 = x2-y2 一遍扫描即可
int main()
{
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long ans = 0;
map<int, int>mp;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
int subs = x - i;
ans += mp[subs];//0+1+2+3+...
mp[subs] += 1;
}
cout << ans << endl;
}
return 0;
}
看了题解后进一步明确代码方案(已经看到x1-y1 = x2-y2 ,没有想到如何统计编码,此题对我来说已经算有一定的难度;但自己思考还是能够做出来)
//x1-x2 = y1-y2 => x1-y1 = x2-y2 一遍扫描即可
//ans += mp[subs];//0+1+2+3+…
反思:不要太高估D的难度,其实已经看到x1-y1 = x2-y2;却没有静下心来去想如何统计次数,太浮躁和不自信了。
注:关于map<int,int>初始化的问题
当第一次取某一个key值的value时,如果value是内置类型(int…),默认的value值为0. 上面D题对map的使用就可以体现。
如果map的value是自定义的类型,且需要自定义初始值时可以在自定义的数据结构中对相应的变量赋初值,详见:
C++——自定义map的value默认值