中南大学第十一届大学生程序设计竞赛-COJ1897-The price table of the snack store

1897: The price table of the snack store

Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 28 Solved: 7
Description
CSUACM team is a group of people who like eating. They go to the market to buy some snacks together. Incidentally, the number of snacks is equal to the number of people, so they make rules as following: Everyone has a kind of favorite snack, which is different from others’. Everyone buys N snacks they like best, and for each kind of snack they don’t like best they buy M snacks. I will tell you that the number of everyone likes best and the cost of everyone. Can you calculate the unit price of every kind of snack?

Input
There are T(T<=10)groups of test data. In each test first line contains three integers K(K<=10000), N , M. K represent the categories number of snacks and the number of people. N and M are described above. (0<=N,M<=1000, n!=m) The next K lines: Every line contains two integers: everyone’s cost Ci(0< Ci<=10^9)and his favorite kinds of snacks Li(1<=Li<=K).

Output
Every test print one line to output every kind of snack’s unit price. (The test data guarantees that every answer is a positive integer).

Sample Input
2
2 2 1
5 1
7 2
3 3 1
8 1
12 3
10 2
Sample Output
1 3
1 2 3
Hint
Source
中南大学第十一届大学生程序设计竞赛

Author
OTTFF

题目大意: k个人对应k种食物,每个人都对应喜欢一种食物,他们会买自己喜欢的食物n个,不喜欢的食物各买m个,告诉你每个人花的钱,求每种食物的单价。
解题思路:
设给出的第i个人喜欢的食物L[i]价格为x[L[i]],总花费 ci
所有食物单价之和为sigmax
nxL[i]+(sigmaxxL[i])m=ci
xL[i]=(cisigmaxm)/(nm)
那如何求sigmax呢?
ki=1(n+(k1)m)xi=ki=1ci
sigmax=xi=(ci)/(n+(k1)m)
这样就可以将每种食物的单价求出来
考察内容:数学,长整数型long long
复杂度: O(n)
题目难度: ★★★

AC代码:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
int L[10005];
LL c[10005],x[10005];

int main()
{
    int T;
    scanf("%d",&T);
    LL sigmax;
    while(T--)
    {
        int k,n,m;
        scanf("%d%d%d",&k,&n,&m);
        sigmax=0;
        for(int i=1;i<=k;i++)
        {
            scanf("%lld%d",&c[i],&L[i]);
            sigmax+=c[i];
        }
        sigmax=sigmax/(n+(k-1)*m);
        for(int i=1;i<=k;i++)
            x[L[i]]=(c[i]-sigmax*m)/(n-m);
        for(int i=1;i<=k;i++)
        {
            if(i==1) printf("%lld",x[i]);
            else printf(" %lld",x[i]);
        }
        printf("\n");
    }
    return 0;
}

RE代码:

#include<iostream>
#include<cmath>
#define eps (1e-9)
using namespace std;
const int MAXN=10005;
double a[MAXN][MAXN],x[MAXN];
int equ,var;

int Gauss()
{
    int i,j,k,col,max_r;
    for(k=0,col=0;k<equ&&col<var;k++,col++)
    {
        max_r=k;
        for(i=k+1;i<equ;i++)
            if(fabs(a[max_r][col])>fabs(a[max_r][col]))
                max_r=i;
        if(fabs(a[max_r][col])<eps) return 0;
        if(k!=max_r)
        {
            for(j=col;j<var;j++)
                swap(a[k][j],a[max_r][j]);
            swap(x[k],x[max_r]);
        }
        x[k]/=a[k][col];
        for(j=col+1;j<var;j++) a[k][j]/=a[k][col];
        a[k][col]=1;
        for(i=0;i<equ;i++)
            if(i!=k)
        {
            x[i]-=x[k]*a[i][k];
            for(j=col+1;j<var;j++) a[i][j]-=a[k][j]*a[i][col];
            a[i][col]=0;
        }
    }
    return 1;
}

int main()
{
    int T;
    cin>>T;
    int k,n,m,w;
    while(T--)
    {
        cin>>k>>n>>m;
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<k;j++)
            {
                a[i][j]=m;
            }
        }
        for(int i=0;i<k;i++)
        {
            cin>>x[i]>>w;
            a[i][w-1]=n;
        }
        equ=k;var=k;
        Gauss();
        for(int i=0;i<k;i++)
        {
            if(i==0)  cout<<x[i];
            else  cout<<" "<<x[i];
        }
        cout<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值