7.10
因为之前的日记都没写,今天一天都在补日记,一天只写了一道题。。。下午的时候打了场个人赛,做出来了三个贪心题;
Kuangbin:简单搜索
A. Find The Multiple(POJ_1426)
传送门
直接暴力枚举,从1开始,因为数字仅由0和1组成,所以每次先* 10
枚举一个,再+ 1
枚举一个;
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX = 100 + 5;
ll n;
void bfs() {
queue<ll> q;
q.push(1);
while(!q.empty()) {
ll now = q.front();
q.pop();
if(now % n == 0) {
cout << now << '\n';
return;
}
ll a = now * 10;
ll b = a + 1;
q.push(a);
q.push(b);
}
}
void solve() {
bfs();
}
void init() {
}
signed main() {
std::ios::sync_with_stdio(false);
while(cin >> n) {
if(!n) {
break;
}
init();
solve();
}
return 0;
}
CodeForces个人赛:Training 2
传送门
只a出来了三题,要是做的再快点儿应该还能再a一题吧;
A. Problem 1
传送门
贪心,一个我也不知道为什么这样写,玄学找规律的贪心,把三个数两两差值都加起来给sum,如果sum < 4
就输出0,否则就输出sum - 4
;
#include <bits/stdc++.h>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX = 1e2 + 5;
ll num[3];
void solve() {
ll sum = 0;
sum = abs(num[1] - num[0]) + abs(num[2] - num[1]) + abs(num[2] - num[0]);
if(sum >= 4)
sum -= 4;
else
sum = 0;
cout << sum << '\n';
}
void init() {
}
int main() {
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--) {
for(int i = 0; i < 3; ++i) cin >> num[i];
init();
solve();
}
return 0;
}
B. Problem 2
传送门
贪心,往左走和往右走的步数(或者往上走和往下走)的步数要相等,且输出的时候,左右(上下)不能挨着;还存在特别情况:如果只剩左右(或者上下),不管有多少对,都只输出LR(UD);
#include <bits/stdc++.h>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX = 2e5 + 5;
string op;
int u, d, l, r;
void solve() {
int ans = op.size();
for (int i = 0; i < op.size(); ++i) {
if (op[i] == 'U') u++;
else if (op[i] == 'D') d++;
else if (op[i] == 'L') l++;
else if (op[i] == 'R') r++;
}
ans -= abs(l - r) + abs(u - d);
int tmp;
tmp = min(u, d);
u = d = tmp;
tmp = min(l, r);
l = r = tmp;
if (l == 0 && u == 0) {
cout << 0 << '\n';
return;
}
else if (l == 0) {
u = d = 1;
cout << "2\nUD\n";
return;
}
else if (u == 0) {
l = r = 1;
cout << "2\nLR\n";
return;
}
cout << ans << '\n';
for (int i = 0; i < l; ++i) cout << 'L';
for (int i = 0; i < u; ++i) cout << 'U';
for (int i = 0; i < r; ++i) cout << 'R';
for (int i = 0; i < d; ++i) cout << 'D';
cout << '\n';
}
void init() {
u = d = l = r = 0;
}
int main() {
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
init();
cin >> op;
solve();
}
return 0;
}
C. Problem 3
传送门
贪心,题目中已经给出了计算一个字符串所有子串个数的公式,所以只用把每一段合法字符串都求其所有子串加起来就是答案;
#include <bits/stdc++.h>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX = 2e5 + 5;
int n, k;
ll ans;
string s;
bool ch[26];
void solve() {
ll cnt = 0;
for (int i = 0; i < s.size(); ++i) {
if (!ch[s[i] - 'a']) {
ans += cnt * (cnt + 1) / 2;
cnt = 0;
continue;
}
cnt++;
}
ans += cnt * (cnt + 1) / 2;
cout << ans << '\n';
}
void init() {
ans = 0;
mms(ch, false);
}
int main() {
std::ios::sync_with_stdio(false);
while (cin >> n >> k) {
init();
cin >> s;
for (int i = 0; i < k; ++i) {
char tmp;
cin >> tmp;
ch[tmp - 'a'] = true;
}
solve();
}
return 0;
}