只会做前三题,第四题听懂怎么做了,但是不懂为什么这么做。。。
A:At Most 3 (Judge ver.)
题意:第一行输入n和w。第二行输入n个a【i】,求一个或两个或三个a[i]的和小于等于w的个数。
7 251 202 20 5 1 4 2 100
太遗憾了,这次比赛就做了一道题。第一道题用暴力用怕了,不敢用,结果只能暴力跑,我傻了,该用还不用。但是比起我开标记数组来做,不如大佬用的set列表,用来去重是真香
//第三次比赛a题
//如果我早知道,入列表也需要花很多时间emm
#include<iostream>
#include<set>
using namespace std;
set<int> s;int i, n,w,a[305],num=0;
int main()
{
scanf("%d%d", &n, &w);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
{
int t = a[i];
for (int j = i + 1; j <= n; j++)
{
int t = a[i] + a[j];
for (int k = j + 1; k <= n; k++)
{
int t = a[i] + a[j] + a[k];
if (t <= w && !s.count(t))
{
num++;
s.insert(t);
}
}
if (t <= w && !s.count(t))
{
num++;
s.insert(t);
}
}
if (t <= w && !s.count(t))
{
num++;
s.insert(t);
}
}
printf("%d", num);
}
B:Poem Online Judge
题意:第一行输入一个n,代表n行数据。每行数据先输入一个字符串,再输入他的权值。优先比较权值,再比较字符串出场顺序。输出第几个出场的获胜。
//第二次比赛b题
//不得不说列表做去重是真的爽
#include<iostream>
#include<set>
#include<cstring>
#define maxn 100010
using namespace std;
int main()
{
set<string> s;
int n,a[maxn],maxx=1;
cin >> n;
for (int i = 1; i <= n; i++)
{
string ss;
cin >> ss >> a[i];
if (a[maxx] < a[i]&&!s.count(ss))
maxx = i;
s.insert(ss);
}
cout << maxx;
}
c题: At Most 3 (Contestant ver.)
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
cout << "300" << endl;
for (int i = 1; i <= n; i++)
printf("%d %d %d ", i, i * 100, i * 10000);
}
D: Takahashi and Animals
花钱喂动物,求画的最少喂所有动物。
喂1和2要花a[1]
喂2和3要花a[2]
........................
喂n-1和n要花a[n-1]
喂n和1要花a[n]
第一行输入a[i]个数,第二行输入权值a[i]
#include<iostream>
using namespace std;
#define maxn 300010
int n;
long long int a[maxn], dp1[maxn][2], dp2[maxn][2];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
dp1[1][0] = 1000000010;
dp1[1][1] = a[n];
dp2[1][1] = 1000000010;
for (int i = 2; i <= n; i++)
{
dp1[i][0] = dp1[i - 1][1];
dp1[i][1] = min(dp1[i - 1][1], dp1[i - 1][0]) + a[i-1];
dp2[i][0] = dp2[i - 1][1];
dp2[i][1] = min(dp2[i - 1][1], dp2[i - 1][0])+a[i-1];
}
long long int res = min(min(dp1[n][0], dp1[n][1]), dp2[n][1]);
printf("%lld", res);
}