2015年江西理工大学C语言程序设计竞赛(高级组)

时间:2021-12-24 03:02:02

A

解法:DP+二分

dp[i]=max(dp[i],dp[j]+p[i].v)(i>j)

dp[i]表示建立i点之后能够获得的最大值

int n,M;

struct node
{
int l,v;
}p[];
int dp[]; bool cmp(node a,node b){
return a.l < b.l;
} bool judge_oo(){
int Max = -;
for(int i = ;i <= n;i++) Max = max(Max,p[i].v);
if(Max >= M) return ;
return ;
} bool judge_no(){
int sum = ;
for(int i = ;i <= n;i++) sum += p[i].v;
if(sum >= M) return ;
return ;
} bool judge_DP(int L){
memset(dp,,sizeof dp);
for(int i = ;i <= n;i++){
for(int j = ;j < i; j++){
if(p[i].l - p[j].l >= L){
dp[i] = max(dp[i],dp[j] + p[i].v);
}
}
}
int ML = ;
for(int i = ;i <= n;i++) ML = max(ML,dp[i]);
if(ML >= M) return ;
return ;
} int solve(int L,int R){
while(L + < R){
int mid = (L + R)/;
if(judge_DP(mid)) L = mid;
else R = mid;
}
return L;
} int main(int argc, char *argv[])
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int T;
cin >> T;
while(T--){
cin >> n >> M;
for(int i = ;i <= n;i ++)
cin >> p[i].l >> p[i].v;
if(judge_no()){
puts("-1");
continue;
}
if(judge_oo()){
puts("oo");
continue;
}
sort(p+,p+n+,cmp);
p[].l = - , p[].v = ;
int L = , R = p[n].l;
cout << solve(L,R) << endl;
}
return ;
}

B

解法:模拟。字符串模拟

#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
int i,j;
int n,m;
int num_1,num_2;
int t;
int sum,ans,flag;
string s_1,s_2,s_3;
string sss;
string ss[10000];
string c;
map<string,int>q1;
map<string,int>q2;
int main()
{
cin>>t;
while(t--)
{
cin>>n>>c;
num_1=0;
for(i=0; i<n; i++)
{
cin>>sss;
ans=sss.find(".");
s_1=sss.substr(0,ans);
s_2=sss.substr(ans+1,sss.length());
if(s_2==c)
{
int ans_2=sss.find("(");
int ans_3=sss.find(")");
if((ans_2!=-1&&ans_2<ans_3))
{
// num_1++;
ss[num_1++]=sss.substr(0,ans_2);
// cout<<sss.substr(0,ans_2)<<endl;
}
else if(ans_2==-1||ans_3==-1)
{
// num_1++;
ss[num_1++]=sss.substr(0,ans);
// cout<<sss.substr(0,ans)<<endl;
}
}
}
for(i=0; i<num_1; i++)
{
// cout<<ss[i]<<"A"<<endl;
}
// cout<<num_1<<endl;
for(i=0; i<num_1; i++)
{
q1[ss[i]]++;
}
for(i=0; i<num_1; i++)
{
if(q2[ss[i]]==0)
{
q2[ss[i]]=1;
if(q1[ss[i]]==1)
{
cout<<ss[i]<<"."<<c<<endl;
}
else
{
cout<<ss[i]<<"."<<c<<" "<<q1[ss[i]]<<endl;
}
}
}
q1.clear();
q2.clear();
}
return 0;
}

C

解法:树状数组寻找逆序对+预处理

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath> using namespace std; const int M = ; long long ans[M + ];
int G[M + ],c[M + ]; int Lowbit(int x)
{
return x & (-x);
} void Insert(int x,int d)
{
while(x<=M){
c[x]+=d;
x+=Lowbit(x);
}
} int Sum(int x)
{
int sum=;
while(x>){
sum+=c[x];
x-=Lowbit(x);
}
return sum;
} void init()
{
memset(G,-,sizeof G);
memset(ans,,sizeof ans);
memset(c,,sizeof c);
G[] = ;
for(int i = ; i <= ;i++){
if(G[i] == -){
for(int j = i;j <= ;j += i){
if(G[j] == -){
G[j] = i;
}
}
}
}
for(int i=;i<=;i++)
{
Insert(G[i],);
ans[i] = ans[i-] + (i-Sum(G[i]));
}
} int main()
{
init();
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<ans[n]<<endl;
}
return ;
}

D

解法:模版题,最小覆盖圆

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const double eps=1e-;
struct Point{
double x,y;
}p[];
double dis(const Point &a,const Point &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point circumcenter(const Point &a,const Point &b,const Point &c)
{ //返回三角形的外心
Point ret;
double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/;
double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/;
double d=a1*b2-a2*b1;
ret.x=a.x+(c1*b2-c2*b1)/d;
ret.y=a.y+(a1*c2-a2*c1)/d;
return ret;
}
void min_cover_circle(Point *p,int n,Point &c,double &r){ //c为圆心,r为半径
random_shuffle(p,p+n); //
c=p[]; r=;
for(int i=;i<n;i++)
{
if(dis(p[i],c)>r+eps) //第一个点
{
c=p[i]; r=;
for(int j=;j<i;j++)
if(dis(p[j],c)>r+eps) //第二个点
{
c.x=(p[i].x+p[j].x)/;
c.y=(p[i].y+p[j].y)/;
r=dis(p[j],c);
for(int k=;k<j;k++)
if(dis(p[k],c)>r+eps) //第三个点
{//求外接圆圆心,三点必不共线
c=circumcenter(p[i],p[j],p[k]);
r=dis(p[i],c);
}
}
}
}
}
int main(){
int n;
Point c;
double r;
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
min_cover_circle(p,n,c,r);
printf("%.1f %.1f\n",c.x,c.y);
} return ;
}