T1
思路:找出每个位置据上下左右距离最短的距离,则为该位置所在的圈数;
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int m,t;
for (int o = 0; o < n; o++)
{
m = o <= n - 1 - o ? o : n - 1 - o;
for (int i = 0; i < n; i++)
{
t = i;
t = t <= n-1-i ? t : n-1-i;
t = t <= m ? t : m;
t += 1;
if (t % 2 == 1)cout << '+';
else cout << ".";
}
cout << endl;
}
}
T2
思路:dp,记录每一层中分别已经走了0,1,2步两层,最后累加最后一层并且输出即可。
#include <iostream>
using namespace std;
int main()
{
int n; cin >> n;
long long f[100][4] = {0};
f[1][0] = 1, f[2][1] = 1, f[2][0] = 1;
for (int i = 3; i <= n; i++)
{
f[i][0] = f[i - 1][0] + f[i - 1][1] + f[i - 1][2];
f[i][1] = f[i - 2][0];
f[i][2] = f[i - 2][1];
}
cout << f[n][0] + f[n][1] + f[n][2];
}
T3
思路:dp,用二维数组表示第行步能否走到第列格,最后输出最后一行的结果即可。
#include <iostream>
#include <iomanip>
using namespace std;
int ans[1005][100005] = { 0 };
int n, m;
int a[100005], b[100005];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> a[i] >> b[i];
}
ans[0][0] = 1;
for (int o = 0; o < n; o++)
{
for (int i = 0; i <= m; i++)
{
if (ans[o][i] == 1 && i + a[o] <= m) ans[o+1][i+a[o]] = 1;
if (ans[o][i] == 1 && i + b[o] <= m)ans[o + 1][i + b[o]] = 1;
}
}
for (int i = 0; i <= m; i++)
{
if (ans[n][i] == 1)cout << '1';
else cout << '0';
}
return 0;
}
思路:一道简单的模拟,注意不要重复添加已经ac过的题目的分数即可。
#include<iostream>
#include<string.h>
using namespace std;
string name[205],text[205];
long long score[205];
int tp[205];
int find_name(string na)
{
for (int i = 0; i < 205; i++)
{
if (na == name[i])return i;
}
}
int find_text(string na)
{
for (int i = 0; i < 205; i++)
{
if (na == text[i])return i;
}
}
int main()
{
for (int i = 0; i < 205; i++)
{
score[i] = 0;
}
int n, m, k;
cin >> n >> m >> k;
string na,t;
char c[2];
for (int i = 0; i < n; i++)
{
cin >> name[i];
}
for (int i = 0; i < m; i++)
{
cin >> text[i] >> tp[i];
}
for (int i = 0; i < k; i++)
{
cin >> na >> t >> c[0];
if (c[0] == 'A')
{
score[find_name(na)] += tp[find_text(t)];
}
if (i < k - 1)cin >> c[1];
}
for (int i = 0; i < n; i++)
{
cout << name[i] << ' ' << score[i] << endl;
}
return 0;
}
T5
思路:注意按从大到小的顺序依次判断是否满足即可
#include<iostream>
#include<string.h>
using namespace std;
int p[6][3];
int num[15];
int hua[5];
int main()
{
for (int i = 0; i < 5; i++)
{
cin >> p[i][1];
num[p[i][1]]++;
}
for (int i = 0; i < 5; i++)
{
cin >> p[i][2];
hua[p[i][2]]++;
}
int i = 0;
for (; i < 4; i++)
{
if (p[i][1] != p[i + 1][1] - 1)break;
if (p[i][2] != p[i + 1][2])break;
}
if (i == 4)
{
if (p[4][1] == 14)cout << "ROYAL FLUSH";
else cout << "STRAIGHT FLUSH";
return 0;
}
int max = 0;
for (i = 1; i <= 14; i++)
{
max = max >= num[i] ? max : num[i];
}
if (max >= 4)
{
cout << "FOUR OF A KIND";
return 0;
}
if (max == 3)
{
for (i = 1; i <= 14; i++)
{
if (num[i] == 2)
{
cout << "FULL HOUSE";
return 0;
}
}
}
for (i = 0; i < 4; i++)
{
if (p[i][2] != p[i + 1][2])break;
}
if (i == 4)
{
cout << "FLUSH";
return 0;
}
for (i = 0; i < 4; i++)
{
if (p[i][1] != p[i + 1][1]-1)break;
}
if (i == 4)
{
cout << "STRAIGHT";
return 0;
}
cout << "FOLD";
return 0;
}
T6
思路:用区间划分,找到上确界大于等于该数的,如果下界不包含x,则将下届为x的新编号;如果包含,则将原区间划分为两个
#include<iostream>
#include<string.h>
#include<set>
#include<utility>
using namespace std;
int n;
set<pair<int,int >>c;
inline void insert(int l, int r)
{
if (l > r)return;
c.insert(make_pair(r, l));
}
int main()
{
scanf("%d",&n);
c.insert(make_pair(2e9,1));
int x;
for (int i = 0; i < n; i++)
{
scanf("%d",&x);
auto it = c.lower_bound(make_pair(x,0));
if (it->second < x)
{
printf("%d ",x);
insert(it->second,x-1);
insert(x+1,it->first);
c.erase(it);
}
else
{
printf("%d ", it->second);
insert(it->second+1,it->first);
c.erase(it);
}
}
return 0;
}
T7
思路:dp,考虑是否采用可执行的任务或者摆烂。
#include<iostream>
#include<string.h>
#include<set>
#include<utility>
#include<queue>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int s[1005], e[1005], w[1005], f[10005] = { 0 };
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", &s[i], &e[i], &w[i]);
}
for (int o = 1; o <= 1000; o++)
{
f[o] = max(f[o], f[o - 1]);
for (int i = 0; i <n; i++)
{
if(s[i]==o)
f[e[i]] = max(f[e[i]], f[o] + w[i]);
}
}
printf("%d", f[1000]);
return 0;
}
思路:先用二分找出可以打完的最大的轮次,然后判断
#include<iostream>
#include<string.h>
#include<set>
#include<utility>
#include<queue>
using namespace std;
int n,a[100005],in[100005];
long long k;
long long ca(int x)
{
long long res = 0;
for (int i = 1; i <= n; i++)
{
if (x >= a[i])res += a[i];
else res += x;
}
return res;
}
long long sum = 0;
int main()
{
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
if (sum < k)
{
cout << "-1";
return 0;
}
int l = 0, r = 1e9;
int mid;
while (l+1< r)
{
mid = (l + r) / 2;
if (ca(mid) <= k)l = mid;
else r = mid;
}
k -= ca(l);
int tot = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] > l)
{
in[++tot] = i;
}
}
for (int i = k + 1; i <= tot; i++)
{
printf("%d ",in[i]);
}
for (int i = 1; i <= k; i++)
{
if(a[i]>=l+1)printf("%d ", in[i]);
}
return 0;
}