T2】接水问题
有一些小细节,比如如果最小值存在多个,比如最后还需要一个完全结束的最大值
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<stack> #include<cstdio> #include<queue> #include<map> #include<vector> #include<set> using namespace std; const int maxn=10100; const int INF=0x3fffffff; int n,m; int b[110]; int a[maxn]; int main(){ cin>>n>>m; int ans=0; for(int i=1;i<=n;i++) cin>>a[i]; int minn=INF,minindex=-1; for(int i=1;i<=m;i++){ b[i]=a[i]; } for(int i=m+1;i<=n;i++){ minn=b[1]; vector<int> minindex; for(int j=1;j<=m;j++){ if(b[j]<=minn){ minn=b[j]; //minindex.push_back(j); } } for(int j=1;j<=m;j++){ if(b[j]==minn) minindex.push_back(j); b[j]-=minn; } ans+=minn; for(int j=0;j<minindex.size();j++) b[minindex[j]]=0; b[minindex[0]]=a[i]; } int maxx=-INF; for(int i=1;i<=m;i++){ if(b[i]>maxx) maxx=b[i]; } ans+=maxx; cout<<ans<<endl; return 0; }
T3 导弹拦截
还是写一下,细节
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 100002, inf = 1000000000; int X1, Y1, X2, Y2, n; struct data { int x, y; long long t1, t2; }; bool cmp(data a, data b) { return a.t1 < b.t1; } data d[maxn]; int main() { scanf("%d%d%d%d", &X1, &Y1, &X2, &Y2); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &d[i].x, &d[i].y); d[i].t1 = (long long)(X1 - d[i].x)*(X1 - d[i].x) + (long long)(Y1 - d[i].y)*(Y1 - d[i].y); d[i].t2 = (long long)(X2 - d[i].x)*(X2 - d[i].x) + (long long)(Y2 - d[i].y)*(Y2 - d[i].y); } sort(d + 1, d + n + 1, cmp); long long r2 = 0,ans=inf; for (int i = n; i >0; i--) { r2 = max(d[i + 1].t2, r2); ans = min(ans, r2 + d[i].t1); } printf("%I64d", ans); return 0; }
T4】三国游戏
这道题不要想复杂了,其实简化了想,就是再求每一行第二大的,然后再求一个最大
就是这样,而且肯定能赢的,肯定
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<stack> #include<cstdio> #include<queue> #include<map> #include<vector> #include<set> using namespace std; const int maxn=1010; const int INF=0x3fffffff; int n; int a[510][510]; int main(){ scanf("%d",&n); int maxx=-INF,mx=0,my=0; for(int i=1;i<=n;i++){ a[i][i]=0; for(int j=i+1;j<=n;j++){ scanf("%d",&a[i][j]); a[j][i]=a[i][j]; } } //找到第二大的 int max1=0,max2=0; int ans=-1; for(int i=1;i<=n;i++){ max1=0,max2=0; for(int j=1;j<=n;j++){ if(a[i][j]>max1){ max2=max1; max1=a[i][j]; } else if(a[i][j]>max2){ max2=a[i][j] ; //存储这一行的第二大的 } } if(max2>ans) ans=max2; //哪一行的第二最大,就存储哪一行的 } cout<<"1"<<endl<<ans<<endl; return 0; }