260C - Balls and Boxes

思路:模拟。在x前面找到最小值,如果没有,从0跳到n,继续找到最小值,边找最小值路过的点边减1。然后所有值都减去最小值,最小值那个点加上减去的值。

找到x前面离x最近的最小值的原因:如果如果在x到最小值之间还有最小值,那么这个最小值最后会变成-1。

简单代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+;
const ll INF=0x7f7f7f7f;
ll a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie();
ll n,x,minn=INF;
cin>>n>>x;
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++)
{
minn=min(minn,a[i]);
}
ll sum=;
while(a[x]!=minn)
{
a[x]--;
sum++;
x--;
if(x==)x=n;
}
for(int i=;i<=n;i++)
{
if(i!=x)cout<<a[i]-minn;
else cout<<a[i]+sum+(n-)*minn;
if(i!=n)cout<<' ';
else cout<<endl;
}
return ;
}

复杂点的代码(找最小值时没有减1):

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+;
const ll INF=0x7f7f7f7f;
ll a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie();
ll n,x,minn=INF,d=INF,index=-;
cin>>n>>x;
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++)
{
if(a[i]<=minn)
{
minn=a[i];
index=i;
}
}
bool flag=true;
for(int i=x;i>=;i--)
{
if(a[i]==minn)
{
index=i;
flag=false;
break;
}
}
if(flag)for(int i=n;i>x;i--)
{
if(a[i]==minn)
{
index=i;
break;
}
}
ll cnt=;
if(index<x)
{
cnt=index*minn+(n-x)*minn+(x-index)*(minn+);
for(int i=;i<=index;i++)a[i]-=minn;
for(int i=index+;i<=x;i++)a[i]-=minn+;
for(int i=x+;i<=n;i++)a[i]-=minn;
a[index]+=cnt;
}
else if(index>x)
{
cnt=x*(minn+)+(n-index)*(minn+)+(index-x)*minn;
for(int i=;i<=x;i++)a[i]-=minn+;
for(int i=x+;i<=index;i++)a[i]-=minn;
for(int i=index+;i<=n;i++)a[i]-=minn+;
a[index]+=cnt;
}
else
{
cnt=n*minn;
for(int i=;i<=n;i++)a[i]-=minn;
a[index]+=cnt;
}
for(int i=;i<n;i++)cout<<a[i]<<' ';
cout<<a[n]<<endl;
return ;
}

Codeforces 260C - Balls and Boxes的更多相关文章

  1. Codeforces Round #158 (Div. 2) C. Balls and Boxes 模拟

    C. Balls and Boxes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. CodeForces - 260C

    CodeForces - 260C Little Vasya had n boxes with balls in the room. The boxes stood in a row and were ...

  3. HDU 5810 Balls and Boxes(盒子与球)

     Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  4. HDU 5810 Balls and Boxes (找规律)

    Balls and Boxes 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5810 Description Mr. Chopsticks is i ...

  5. HDU5810 Balls and Boxes

    Balls and Boxes                                                                            Time Limi ...

  6. HDU 5810 Balls and Boxes 数学

    Balls and Boxes 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5810 Description Mr. Chopsticks is i ...

  7. hdu-5810 Balls and Boxes(概率期望)

    题目链接: Balls and Boxes Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/O ...

  8. hdu 5810 Balls and Boxes 二项分布

    Balls and Boxes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. 2016 多校联赛7 Balls and Boxes(概率期望)

    Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...

随机推荐

  1. 2:2 strus2的配置文件

    strus2 的xml配置文件主要负责Action的管理,常放在WEB-INF/classes目录下,被自动加载 在strus-core jar包下找dtd文件,里面有xml的头信息.也有contan ...

  2. 1:4 UI标签和通用标签

          UI标签:负责用户界面输出的标签. 非标单:例如错误信息提示的标签 fielderror,actionerror,actionmessagr:系统错误消息的自动显示           通 ...

  3. 好消息啊,有些c似乎不用加厂商前缀了

    今天写程序,一个不小心,没写厂商前缀,然而,,,,,,, 可以运行了(我居然不知道这个消息!!!!) 赶紧写几个字纪念一下. (把鼠标移动到红色的字上边~~~) don' you think that ...

  4. 1初识QT

    创建程序步骤: 打开Qt Creator 界面选择 New Project或者选择菜单栏 [文件]-[新建文件或项目]菜单项 弹出New Project对话框,选择Qt Widgets Applica ...

  5. Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 5. Dynamic Programming

    10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=sho ...

  6. python利用WMI监控windows状态如CPU、内存、硬盘

    安装pywin32库 下载地址: https://sourceforge.net/projects/pywin32/files%2Fpywin32/选择对应python版本的文件.下载后在window ...

  7. 519. Random Flip Matrix(Fisher-Yates洗牌算法)

    1. 问题 给定一个全零矩阵的行和列,实现flip函数随机把一个0变成1并返回索引,实现rest函数将所有数归零. 2. 思路 拒绝采样 (1)先计算矩阵的元素个数(行乘以列),记作n,那么[0, n ...

  8. HDFS文件操作

    hadoop装好后,文件系统中没有任何目录与文件 1. 创建文件夹 hadoop fs -mkdir -p /hkx/learn 参数-p表示递归创建文件夹 2. 浏览文件 hadoop fs -ls ...

  9. ajax return true/false无效原因

    错误示例:function checkCP(customerId,productId){ $.ajax({ url:"/cp/checkCP", type:"post&q ...

  10. Js删除字符串中的指定字符串

    案例一. 比如:原字符串 var StringFirst = "12:30:08"; 现在要删掉冒号,变成123008 就可以先split var splitFirst = Str ...