牛客周赛99
Round 99
直接遍历看看有没有两个相同的9(int/string都可以)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){ios::sync_with_stdio(false); // 禁用同步cin.tie(nullptr); // 解除cin与cout绑定string s;cin >> s;int i;for ( i = 1; i < s.size(); i++) {if (s[i - 1] == s[i] && s[i] == '9') {cout << "YES" << endl;break;}}if (i == s.size()) {cout<<"NO"<<endl;}return 0;
}
缺陷型电脑
遍历找到ASCLL 在int型下最大的值
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t, n;
string s;
int max_s = 0;
int main() {ios::sync_with_stdio(false); // 禁用同步cin.tie(nullptr); // 解除cin与cout绑定cin >> t;while (t--) {cin >> n >> s;max_s = 0;for (int i = 0; i < n; i++) {if (max_s < s[i]) {max_s = (int)s[i];}}cout << max_s << endl;}return 0;
}
小苯的洞数构造
题目的意思就是0——9组成的数的权值(相当于)等于K,且这个数最小
分三种情况:n==0 1
n 为奇数 4后接n/2个8 (4,48,488,……)
n为偶数 n/2个8(8,88,……)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t, n;
int main() {ios::sync_with_stdio(false); // 禁用同步cin.tie(nullptr); // 解除cin与cout绑定cin >> t;while (t--) {cin >> n;if (n == 0) {cout << 1 << endl;}
// else if (n == 1) {
// cout << 4 << endl;
// }else {if (n % 2 == 1) {for (int i = 0; i < (n + 1)/2; i++) {if (i == 0) {cout << 4;}else {cout << 8;}}cout << endl;}else {for (int i = 0; i < n / 2; i++) {cout << 8;}cout << endl;}}}return 0;
}
前缀和
找规律:当p%x==0 为奇数 2*(p / x)- 1
p%x!=0 为偶数 2 * (p - p / x)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll t, x, p;
int main() {ios::sync_with_stdio(false); // 禁用同步cin.tie(nullptr); // 解除cin与cout绑定cin >> t;while (t--) {cin >> x >> p;if (p % x == 0) {cout << 2*(p / x)- 1 << endl;}else {cout << 2 * (p - p / x) << endl;}}return 0;
}
小宇
我这题的解题思路可能不是特别好
- 遍历数组
a
,对于每个元素a[i]
:- 如果
a[i] == i
,说明该元素已经在正确的位置,不需要修改,跳过。 - 否则,统计该元素出现的次数。如果是第一次出现,
sum
(操作次数)加1,并在q
中记录该元素出现次数为1;如果已经出现过,则增加其出现次数。 - 从后往前遍历数组
a
,检查是否可以减少操作次数:- 如果
a[i] > i
且a[i] > a[i-1]
,并且该元素在q
中只出现一次(it->second == 1
),则可以通过一次操作将其修改为i
,从而减少操作次数(sum--
)。 - 如果
a[i] > i
但a[i] <= a[i-1]
,或者该元素出现多次,则无法通过当前操作减少次数,直接跳出循环。
- 如果
- 如果
大概就是它(后面出现一直a[i] > i && a[i] > a[i - 1]且只出现一次时)或(本身就为a[i])可以不用变为i,剩下的都要变。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t, n;
int a[200005];
int main() {ios::sync_with_stdio(false); // 禁用同步cin.tie(nullptr); // 解除cin与cout绑定cin >> t;while (t--) {map<int,int> q;cin >> n;int sum = 0;a[0] = 0;for (int i = 1; i <= n; i++) {cin >> a[i];}for (int i = 1; i <= n; i++) {if (a[i] == i) {continue;}else {auto it = q.find(a[i]);if (it == q.end()) {sum++;q.insert({ a[i],1 });}else {it->second++;}}}for (int i = n; i > 0; i--) {auto it = q.find(a[i]);if (a[i] > i && a[i] > a[i - 1] && it!=q.end()&&it->second == 1) {sum--;}else {if (a[i] > i && it != q.end()&& it->second == 1) {sum--;}break;}}cout << sum << endl;}return 0;
}
汉堡猪猪分糖果
考虑按高位到低位贪心,若到某一位时剩余数量不足以分给 m 个人,那么只给一部分人在该位进行分配,使得剩下的糖果可以在下一位恰好均分 m 份。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll t, n, m;
int main() {ios::sync_with_stdio(false); // 禁用同步cin.tie(nullptr); // 解除cin与cout绑定cin >> t;while (t--) {cin >> n >> m;int q = 1<<30;int sum = 0;while (q > 0) {if (q * m <= n) {n -= q * m;sum += q;}else {if (m * (q - 1) <= n) {int ff = n - (q - 1) * m;n -= q * ((ff + q - 1) / q);}}q >>=1;}cout << sum << endl;}return 0;
}