4.22周二下午训练 -- 东塘407 总结
Moonlight_dreams · · 个人记录
成绩
T2
P1055 [NOIP 2008 普及组] ISBN 号码
思路
一个比较简单的模拟题。
因为题目是让我们判断识别码对不对,而且给了我们求识别码的方法,所以我们就可以把正确的识别码求出来,然后去判断输入进来的识别码是不是一样的,如果是,就输出 Right,否则输出正确的ISBN 号码。
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int ans = 0 , cnt = 0;
int lens = s.size();
int shi = 0;
string t = "";
for (int i = 0; i < lens; i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
cnt++;
ans += (s[i] - '0') * cnt;
}
if (i == lens - 1)
{
if (s[i] == 'X')
{
shi = 10;
}
else
{
shi = s[i] - '0';
}
break;
}
t += s[i];
}
ans %= 11;
if (ans == 10)
{
if (shi == 10)
{
cout << "Right";
}
else
{
cout << t << 'X';
}
}
else
{
if (ans != shi)
{
cout << t << ans;
}
else
{
cout << "Right";
}
}
return 0;
}
错误原因
在求当前字符是不是数字字符的时候,应该加上一个 i != lens - 1。因为我们算识别码的时候,肯定不会将识别码一起算进去。痛失60分呜呜呜。
AC 代码如下
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int ans = 0 , cnt = 0;
int lens = s.size();
int shi = 0;
string t = "";
for (int i = 0; i < lens; i++)
{
if (i != lens - 1 && s[i] >= '0' && s[i] <= '9')
{
cnt++;
ans += (s[i] - '0') * cnt;
}
if (i == lens - 1)
{
if (s[i] == 'X')
{
shi = 10;
}
else
{
shi = s[i] - '0';
}
break;
}
t += s[i];
}
ans %= 11;
// cout << ans;
if (ans == 10)
{
if (shi == 10)
{
cout << "Right";
}
else
{
cout << t << 'X';
}
}
else
{
// cout << ans << " " << shi << endl;
if (ans != shi)
{
cout << t << ans;
}
else
{
cout << "Right";
}
}
return 0;
}
T3
P4826 [USACO15FEB] Superbull S
杨老师作业题
思路
很模板的最大生成树awa
-
kruskal 函数 直接写最小生成树模板。
① 并查集初始化。
② 按照
val 从大到小排序。③ 枚举所有边,然后去求和。
void kruskal()
{
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
sort(s + 1 , s + cnt + 1 , cmp);
for (int i = 1; i <= cnt; i++)
{
int x = get(s[i].x) , y = get(s[i].y);
if (x == y)
{
continue;
}
unite(x , y);
ans += s[i].val;
}
return ;
}
-
主函数 主要讲
val 的计算。因为题目要求每个团队都要和其他团队比一场赛,我们就可以双重
for 循环枚举所有点,val = a[i] ^a[j] 。
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
struct stu
{
int x , y , val;
}s[N];
int fa[N] , a[N];
int get(int x)
{
if (x == fa[x])
{
return x;
}
return fa[x] = get(fa[x]);
}
void unite(int x , int y)
{
x = get(x);
y = get(y);
if (x != y)
{
fa[x] = y;
}
return ;
}
bool cmp(stu x , stu y)
{
return x.val > y.val;
}
int n;
int ans = 0 , cnt = 0;
void kruskal()
{
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
sort (s + 1 , s + cnt + 1 , cmp);
for (int i = 1; i <= cnt; i++)
{
int x = get(s[i].x) , y = get(s[i].y);
if (x == y)
{
continue;
}
unite(x , y);
ans += s[i].val;
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i != j)
{
cnt++;
s[cnt].x = i;
s[cnt].y = j;
s[cnt].val = (a[i] ^ a[j]);
}
}
}
kruskal();
cout << ans;
return 0;
}
错误原因
RE 了,数组开小了,2000
AC 代码如下
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e6 + 5;
struct stu
{
int x , y , val;
}s[N];
int fa[N] , a[N];
int get(int x)
{
if (x == fa[x])
{
return x;
}
return fa[x] = get(fa[x]);
}
void unite(int x , int y)
{
x = get(x);
y = get(y);
if (x != y)
{
fa[x] = y;
}
return ;
}
bool cmp(stu x , stu y)
{
return x.val > y.val;
}
int n;
int cnt = 0 , ans;
void kruskal()
{
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
sort(s + 1 , s + cnt + 1 , cmp);
for (int i = 1; i <= cnt; i++)
{
int x = get(s[i].x) , y = get(s[i].y);
if (x == y)
{
continue;
}
unite(x , y);
ans += s[i].val;
}
return ;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
continue;
}
cnt++;
s[cnt].x = i;
s[cnt].y = j;
s[cnt].val = (a[i] ^ a[j]);
}
}
kruskal();
cout << ans;
return 0;
}
T4
P2058 [NOIP 2016 普及组] 海港
思路
这道题可以用队列来模拟~~
我们首先可以将所有的不同的国籍计算出来,每找到一个新的国籍,答案 + 1,然后去判断如果队列中的第一个元素超过了 24 小时,就判断这一个国籍是不是只有这一个,如果是,就答案 - 1。
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int a[N];
map <int , int> mp;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
int cnt = 0;
while (T--)
{
int t , k;
cin >> t >> k;
for (int i = 1; i <= k; i++)
{
cin >> a[i];
mp[a[i]]++;
if (mp[a[i]] == 1)
{
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
错误原因
没有判断有没有超过 24 小时。
AC 代码如下
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int cnt[N];
queue <int> qu , que;
signed main()
{
int T;
cin >> T;
int ans = 0;
while (T--)
{
int t , k;
cin >> t >> k;
for (int i = 1; i <= k; i++)
{
int x;
cin >> x;
qu.push(t);
que.push(x);
cnt[x]++;
if (cnt[x] == 1)
{
ans++;
}
}
while (t - qu.front() >= 86400)
{
int x = que.front();
que.pop();
qu.pop();
cnt[x]--;
if (cnt[x] == 0)
{
ans--;
}
}
cout << ans << endl;
}
return 0;
}
T5
P1582 倒水
思路
我们可以来分析一下,以 1 ~ 5 为例子
原来的瓶子数量 转换为二进制 合并之后的瓶子数量
1 1 1
2 10 1
3 11 2
4 100 1
5 101 2
我们从上面的分析可以知道,其实合并之后的瓶子数量就是原来的瓶子数量的二进制形式中 1 的数量。
我们只要找到一个数是2的幂次方,且大于 k,然后在统计答案即可。
AC 代码如下
#include <bits/stdc++.h>
#define int long long
using namespace std;
int getnum1(int x)
{
int cnt = 0;
for (int i = 32; i >= 0; i--)
{
if ((x >> i) & 1)
{
cnt++;
}
}
return cnt;
}
int getlower1(int x)
{
for (int i = 0; i <= 32; i++)
{
if ((x >> i) & 1)
{
return i;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , k;
cin >> n >> k;
int ans = 0;
while (getnum1(n) > k)
{
int cnt = getlower1(n);
ans += (1 << cnt);
n += (1 << cnt);
}
cout << ans;
return 0;
}