T1 P1051 [NOIP 2005 提高组] 谁拿了最多奖学金
这道题没什么难度,只要按他的判断就可以了。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n;
struct stu{
string m;
int qc, bc, l;
char g, x;
long long z=0;
}a[101];//每个人的数据
int main(){
cin >> n;
int ans=0;
for(int i=1;i<=n;i++){
cin >> a[i].m >> a[i].qc >> a[i].bc >> a[i].g >> a[i].x >> a[i].l;
if(a[i].bc>80 && a[i].g=='Y') a[i].z+=850;//班级贡献奖
if(a[i].qc>85 && a[i].x=='Y') a[i].z+=1000;//西部奖学金
if(a[i].qc>90) a[i].z+=2000;//成绩优秀奖
if(a[i].qc>85 && a[i].bc>80) a[i].z+=4000;//五四奖学金
if(a[i].qc>80 && a[i].l) a[i].z+=8000;//院士奖学金
ans+=a[i].z;
}
string ming=a[1].m;
int maxn=a[1].z;
for(int i=1;i<=n;i++){
if(maxn<a[i].z){
ming=a[i].m;
maxn=a[i].z;
}
}
cout << ming << "\n" << maxn << "\n" << ans;
return 0;
}T2 P1616 疯狂的采药
这道题没什么难度,完全背包,但是,在考试时我不知怎么了应该是脑子瓦特了,就在后面写了一堆奇奇怪怪的东西。
60分代码:
#include<bits/stdc++.h>
using namespace std;
const int M=1e7+5;
int m, n;
int f1[M], f2[M];
int w[M], v[M];
int main(){
cin >> m >> n;
for(int i=1;i<=n;i++) cin >> w[i] >>v[i];
int maxn=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(j>=w[i]){
f2[j]=max(f1[j], f2[j-w[i]]+v[i]);
}
else f2[j]=f1[j];
maxn=max(maxn, f2[j]);
}
for(int j=1;j<=m;j++) swap(f1[i], f2[i]);
}
cout << maxn;
return 0;
}“AC”代码(仅90)
#include<bits/stdc++.h>
using namespace std;
const int M=1e7+5;
const int N=1e4+5;
int n, m;
int w[N], v[N];
long long f1[M], f2[M];
signed main(){
cin >> m >> n;
for(int i=1;i<=n;i++) cin >> w[i]>> v[i];
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
if(j>=w[i]) f1[j]=max(f2[j], f1[j-w[i]]+v[i]);
else f1[j]=f2[j];
}
for(int j=0;j<=m;j++) f2[j]=f1[j];
}
cout << f1[m];
return 0;
}T3 P1464 Function
这个简单,按他的来就可以了,记得要记忆化,应该还要long long。
AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a, b, c;
int aa[21][21][21];
int w(int a, int b, int c){
if(a<=0 || b<=0 || c<=0) return 1;
else if(a>20 || b>20 | c>20) return w(20, 20, 20);
else if(aa[a][b][c]) return aa[a][b][c];//记忆化
else if(a<b && b<c) aa[a][b][c]=w(a, b, c-1)+w(a, b-1, c-1)-w(a, b-1, c);
else aa[a][b][c]=w(a-1, b, c)+w(a-1, b-1, c)+w(a-1, b, c-1)-w(a-1, b-1, c-1);
return aa[a][b][c];
}
signed main(){
while(cin >> a >> b >> c){
if(a==-1 && b==-1 && c==-1) return 0;;
cout << "w(" << a << ", " << b << ", " << c << ") = " << w(a, b, c) << "\n";
}
return 0;
}T4 P1443 马的遍历
这个简单,就是bfs板子。也不知为何TLE了一个点,可能哪里出了点“小”问题吧。
TLE90分代码:
#include<bits/stdc++.h>
using namespace std;
int dx[]={-2, -1, 1, 2, 2, 1, -1, -2};
int dy[]={ 1, 2, 2, 1, -1, -2, -2, -1};
struct stu{
int x, y, step;
};
queue<stu> q;
bool vis[405][405];
int ex, ey, sx, sy, n, m;
int bfs(){
q.push({sx, sy, 0});
vis[sx][sy]==1;
while(!q.empty()){
stu t=q.front();
q.pop();
if(t.x==ex && t.y==ey) return t.step;
for(int i=0;i<8;i++){
int nx=t.x, ny=t.y;
nx+=dx[i];
ny+=dy[i];
if(nx<1 || nx>n || ny<1 || ny>m || vis[nx][ny]) continue;
q.push({nx, ny, t.step+1});
vis[nx][ny]=1;
}
}
return -1;
}
int main(){
cin >> n >> m >> sx >> sy;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
ex=i;
ey=j;
cout << bfs() << setw(4) ;
while(!q.empty()) q.pop();
memset(vis, 0, sizeof(vis));
}
cout << "\n";
}
return 0;
}AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 201;
int v[maxn][maxn];
int n, m, mx[8]={1, 1, -1, -1, 2, 2, -2, -2}, my[8] = {2, -2, 2, -2, 1, -1, 1, -1};
struct s{int x, y, n;} p;
queue<s> q;
int main() {
int i, j;
cin >> n >> m;
for (i = 0; i < n; i++) for (j = 0; j < m; j++) v[i][j] = -1;
cin >> p.x >> p.y;
p.x--;
p.y--;
v[p.x][p.y] = 0;
q.push(p);
while (!q.empty()) {
s t;
for (i = 0; i < 8; i++) {
t = q.front();
int x = t.x + mx[i];
int y = t.y + my[i];
if (x < 0 || x > n || y < 0 || y > m || v[x][y] != -1) continue;
t.x += mx[i];
t.y += my[i];
t.n++;
q.push(t);
v[t.x][t.y] = t.n;
}
q.pop();
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) cout<< v[i][j] << " ";
cout << endl;
}
return 0;
}T5 P1832 A+B Problem(再升级)
从这题开始就没时间做了,都磕在1~4题了…
这题其实很水,筛一下质数,然后就是求完全背包方案数。
后AC代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1005;
int n, vis[N];
int cnt, z[N];
int f[N][N];
signed main(){
cin >> n;
for(int i=2;i<=n;i++){
if(!vis[i]){
z[++cnt]=i;
for(int j=i*2;j<=n;j+=i) vis[j]=1;
}
}
f[0][0]=1;
for(int i=1;i<=cnt;i++){
f[i][0]=1;
int w=z[i];
for(int j=1;j<=n;j++){
if(j>=w) f[i][j]=f[i-1][j]+f[i][j-w];
else f[i][j]=f[i-1][j];
}
}
cout << f[cnt][n];
return 0;
}
T6 狗哥玩木棒
这题其实不易不难,只需分四个盒子,深搜列举把木棍放盒子里,若四个盒子里木棍总长一样长,即可。(当然,这些神奇的木棍无法折断,但是所有木棍总长%4!=0,自然无法组合出,就直接no。)
后AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N=25;
int a[N], box[N];
int n, tot;
bool flag;
bool cmp(int q,int h){return q>h;}
void dfs(int step){
if(flag) return ;
if(step>n){
flag=1;
return;
}
for(int i=1;i<=4;i++){
if(box[i]-a[step]>=0){
box[i]-=a[step];
dfs(step+1);
box[i]+=a[step];
}
}
return;
}
int main(){
int T;
cin >> T;
while(T--){
flag=0;
tot=0;
memset(box,0,sizeof box);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i], tot+=a[i];
if(tot%4){
puts("no");
continue;
}
for(int i=1;i<=4;i++) box[i]=tot/4;
sort(a+1,a+n+1,cmp);
dfs(1);
if(flag) puts("yes");
else puts("no");
}
return 0;
}
后两个应该不用搞