题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1398

题意:

输入t 有t个测试用例每个测试用例第一行输入三个数n,m,y;第二行输入n个数x1,x2....xn。

要求输出看k1,k2....kn。使得 |Xi/Y - Ki/M|. 最小,x1+x2+...+xn=y;  k1+k2+...+kn=m;

这个题目是应该做出来的,基础搞好,做什么都好

每个人开始得到  ki=m*xi/y     这样使得m可能还有剩余把|Xi/Y - Ki/M|. 假设把多余的加一个一到上面相减再排序,小的优先加一

如:

sum=m-sum;
for(i=0;i<n;i++)
{
a[i].num=i;
a[i].x=abs((c[i]+1.0)/m-b[i]*1.0/y)-abs(c[i]*1.0/m-b[i]*1.0/y);
}
sort(a,a+n,cmp);
for(i=0;i<sum;i++)
{
c[a[i].num]++;
}

只要有一定的算法基础,这个题就很容易:

代码

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

struct node
{
int num;
double x;
}a[1005];

bool cmp(node a,node b)
{
return a.x<b.x;
}

int main(void)
{
int t;
int i,j,k;
int n,m,y;
int b[1005];
int c[1005];
scanf("%d",&t);
while(t--)
{
int sum=0;
scanf("%d%d%d",&n,&m,&y);
for(i=0;i<n;i++)
{
scanf("%d",b+i);
c[i]=b[i]*m/y;
sum=sum+c[i];
}
sum=m-sum;
for(i=0;i<n;i++)
{
a[i].num=i;
a[i].x=abs((c[i]+1.0)/m-b[i]*1.0/y)-abs(c[i]*1.0/m-b[i]*1.0/y);
}
sort(a,a+n,cmp);
for(i=0;i<sum;i++)
{
c[a[i].num]++;
}
for(i=0;i<n-1;i++)
printf("%d ",c[i]);
printf("%d\n",c[i]);
if(t)
printf("\n");
}
return 0;
}

这个题应注意别改变它相应的次序:

ZOJ-2343-Robbers的更多相关文章

  1. 训练赛第二场G题 ZOJ 2343

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2343 解题报告:首先我假设最后的正确的结果是a[1] , a[2 ...

  2. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  3. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  4. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  5. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  6. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  7. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  8. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

  9. zoj 1788 Quad Trees

    zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...

  10. ZOJ 1958. Friends

    题目链接: ZOJ 1958. Friends 题目简介: (1)题目中的集合由 A-Z 的大写字母组成,例如 "{ABC}" 的字符串表示 A,B,C 组成的集合. (2)用运算 ...

随机推荐

  1. Fragment在Activity中的应用 (转载)

    原文链接 http://www.cnblogs.com/nanxin/archive/2013/01/24/2875341.html 在本小节中介绍在Activity中创建Fragment. 官网有很 ...

  2. Android之TextView控件的学习

    <TextView android:id="@+id/tv"             //id号,指明这个TextView的唯一身份 android:autoLink=&qu ...

  3. usb免驱动摄像头实验

    1.编译openwrt系统内核使它支持usb,进入在/openwrt/trunk上执行make menuconfig 2.1). 添加USB 相关支持Kernel modules —> USB ...

  4. unity3d 多人寻路堵塞堆叠问题

    使用unity提供的NavMeshAgent寻路,当有多个agent一起寻路时总会出现堵塞堆叠的问题. 本人使用了一个非常粗劣简单的方案解决此问题,当然跟魔兽的寻路完全没得比,但保证有比较好的性能,且 ...

  5. DataGridView很详细的用法

    DataGridiew用法总结 一.DataGridView 取得或者修改当前单元格的内容: 当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 ...

  6. Tickets 基础DP

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  7. codeforce 611C New Year and Domino

    n*n预处理. 询问的时候用容斥,再删除边界. #include<cstdio> #include<cstring> #include<cmath> #includ ...

  8. CentOS 7 x64 docker 使用点滴

    CentOS 7 安装 docker yum search docker  查看是否 有docker包 yum info docker  版本为1.3.2 yum -y install docker ...

  9. hibernate--student_course_score

    学生, 课程,分数的设计. a)使用联合主键@EmbeddedId 使用Serializable接口 b)不适用联合主键 联合主键: 3张表, student: id, name course: id ...

  10. ibatis resultMap 的用法

    先看个具体的例子: <resultMap id=”get-product-result” class=”com.ibatis.example.Product”> <result pr ...