程设2022期末
A.最长下坡
#include <cstdio>
int a[1000];
int main(){
int n, ans = 0, now = 0;scanf("%d", &n);
for(int i = 1; i <= n; ++i){
int x;scanf("%d", &x);a[i] = a[n + i] = x;
}
for(int i = 2; i <= n * 2; ++i){
if (a[i] < a[i - 1]) ++now;
else{
if (now > ans) ans = now;
now = 0;
}
}
printf("%d\n", ans);
return 0;
}
B.斗地主大师
#include <iostream>
#include <cstdio>
using namespace std;
long long inf = 1000000000;
long long p, q, x, y, ans = inf;
void dfs(long long now, long long cnt){
if (cnt > 52) return;
if (now == p){
ans = min(ans, cnt);
return;
}
if (now % y == 0) dfs(now / y, cnt + 1);
dfs(now + x, cnt + 1);
}
int main(){
cin>>p>>q>>x>>y;
dfs(q, 0);
if (ans == inf) cout<<"Failed"<<endl;
else cout<<ans<<endl;
return 0;
}
C.通配符匹配
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool f(string s, string p){
int len = s.length();
for (int i = 0; i < len; ++i){
if (p[i] == s[i]);
else if (p[i] == '?');
else if(p[i] != '*') return false;
else{
int tmp = i;
while (p[tmp] == '*') ++tmp;
for (int j = i; j <= len; ++j)
if (f(s.substr(j, len - j), p.substr(tmp, len - tmp)))
return true;
return false;
}
}
if(p.length() == len) return true;
return false;
}
int main(){
int T;cin>>T;while(T--){
string s, p;cin>>s>>p;
if (f(s, p)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
D.薅羊毛
用背包的思想,f数组记录到达 f[i] 有几种方法,g数组记录当前这一步从哪里转移。
(感谢lyt倾情讲解orz
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = 1000000000;
int n, m, maxx = inf, num;
int a[2000], f[50000], g[50000];
void print(int i){
cout<<i<<' '<<f[i]<<endl;
vector<int> v;
for (int j = i; j; j = g[j]){
v.push_back(j);
}
int sum = 0, cnt = 1;
while (!v.empty()){
// cout<<v.back()<<' ';v.pop_back();
int now = v.back() - sum;v.pop_back();
while (a[cnt] != now) ++cnt;
cout<<cnt<<' ';sum += now;
}
cout<<endl;
}
signed main(){
cin>>n>>m;
for (int i = 1; i <= n; ++i){
cin>>a[i];
if (a[i] < m) continue;
if (a[i] == maxx) ++num;
if (a[i] < maxx) num = 1, maxx = a[i];
}
f[0] = 1;
for (int i = 1; i <= n; ++i){
for (int j = m * 2 - 1; j >= a[i]; --j){
if (f[j - a[i]]){
f[j] += f[j - a[i]];
g[j] = j - a[i];
}
}
}
for (int i = m; i < m * 2; ++i){
if (f[i]){
if (f[i] == 1) print(i);
else cout<<i<<' '<<f[i]<<endl;
return 0;
}
}
if (num == inf) cout<<0<<endl;
else cout<<num<<endl;
return 0;
}
F.发放年终奖
#include <cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[200000];
int main(){
int T;cin>>T;while(T--){
int n, k, cnt = 1, b = 0;cin>>n>>k;
for(int i = 1;i <= n;++i){
cin>>a[i];
}
sort(a + 1, a + n + 1);
for(int i = n - 1; i >= 1; --i){
if (k >= a[n] - a[i]){
k -= a[n] - a[i];
++cnt;
}
else{
cout<<cnt<<endl;
b = 1;break;
}
}
if(!b) cout<<n<<endl;
}
return 0;
}