比赛的时候没想明白。。。
A. The Child and Homework
三短一长选一长,三长一短选一短,否则选c
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1010;
string a[10];
bool cmp(string c,string b)
{
return c.size()<b.size();
}
int main()
{
for(int i=0;i<4;i++)getline(cin,a[i]);
sort(a,a+4,cmp);
//for(int i=0;i<4;i++)cout<<a[i]<<endl;
bool flag1=false,flag2=false;
if((a[0].size()-2)*2<=(a[1].size()-2))flag1=true;
if((a[3].size()-2)>=2*(a[2].size()-2))flag2=true;
if(flag1==flag2)printf("C\n");
else if(flag1)cout<<a[0][0]<<endl;
else if(flag2)cout<<a[3][0]<<endl;
else cout<<"C"<<endl;
return 0;
}
B. The Child and Set
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=100010;
struct node
{
int x;
int num;
}a[maxn];
bool cmp(node c,node d)
{
return c.x>d.x;
}
int sum,limit;
int main()
{
//freopen("in.txt","r",stdin);
for(int i=1;i<maxn;i++)
{
a[i].num=i;
a[i].x=(i&(-i));
}
sort(a+1,a+maxn,cmp);
scanf("%d%d",&sum,&limit);
vector<int> ans;
for(int i=1;i<=100000;i++)
{
if(sum==0)break;
if(a[i].num<=limit)
{
if(sum>=a[i].x)
{
sum-=a[i].x;
ans.push_back(a[i].num);
}
}
}
if(sum==0)
{
int len=ans.size();
printf("%d\n",len);
for(int i=0;i<len;i++)cout<<ans[i]<<" ";
}
else cout<<-1;
cout<<endl;
return 0;
}
C. The Child and Toy
贪心,对于每条边选少的那个
仔细想想是对的,先对边进行排序,小的肯定已经被消除了,那就不会产生其他影响。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1010;
int n,m;
int a[maxn];
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int u,v;
int sum=0;
while(m--)
{
scanf("%d%D",&u,&v);
if(a[u]>a[v])sum+=a[v];
else sum+=a[u];
}
printf("%d\n",sum);
return 0;
}
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains ai animals in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads.
Our child is very smart. Imagine the child want to go from area p to area q. Firstly he considers all the simple routes from p to q. For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as f(p, q). Finally, the child chooses one of the routes for which he writes down the value f(p, q).
After the child has visited the zoo, he thinks about the question: what is the average value of f(p, q) for all pairs p, q (p ≠ q)? Can you answer his question?
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105). Then follow m lines, each line contains two integers xi and yi (1 ≤ xi, yi ≤ n; xi ≠ yi), denoting the road between areas xi and yi.
All roads are bidirectional, each pair of areas is connected by at most one road.
Output a real number — the value of .
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
4 3 10 20 30 40 1 3 2 3 4 3
16.666667
3 3 10 20 30 1 2 2 3 3 1
13.333333
7 8 40 20 10 30 20 50 40 1 2 2 3 3 4 4 5 5 6 6 7 1 4 5 7
18.571429
Consider the first sample. There are 12 possible situations:
- p = 1, q = 3, f(p, q) = 10.
- p = 2, q = 3, f(p, q) = 20.
- p = 4, q = 3, f(p, q) = 30.
- p = 1, q = 2, f(p, q) = 10.
- p = 2, q = 4, f(p, q) = 20.
- p = 4, q = 1, f(p, q) = 10.
Another 6 cases are symmetrical to the above. The average is .
Consider the second sample. There are 6 possible situations:
- p = 1, q = 2, f(p, q) = 10.
- p = 2, q = 3, f(p, q) = 20.
- p = 1, q = 3, f(p, q) = 10.
Another 3 cases are symmetrical to the above. The average is .
思路:并查集,首先把边按照从大到小排序,然后遍历,每次加边的时候经过该边的点对为sum[x]*sum[y],因为该边的权值是连接两个集合的最大值,所以保证了结果(连点之间最小值的最大值)的正确性
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct node
{
int u,v;
int w;
node(int x,int y,int f):u(x),v(y),w(f){}
node(){}
}e[maxn];
int n,m,x,y;
int a[maxn],pra[maxn],sum[maxn];
bool cmp(node c,node d)
{
return c.w>d.w;
}
int find(int x)
{
if(x==pra[x])return x;
return pra[x]=find(pra[x]);
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
pra[i]=i;
sum[i]=1;
}
double ans=0;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
e[i]=node(x,y,min(a[x],a[y]));
}
sort(e,e+m,cmp);
for(int i=0;i<m;i++)
{
x=find(e[i].u);
y=find(e[i].v);
if(x!=y)
{
ans+=(double)e[i].w*sum[x]*sum[y];
pra[y]=x;
sum[x]+=sum[y];
}
}
printf("%.6lf\n",ans/(n*1.0*(n-1.0)/2.0));
return 0;
}