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. 定时器事件QtimerEvent 随机数 qrand Qtimer定时器

    QTimerEvent类:定时器事件.QObject的子类都可使用  int QObject::startTimer(int interval)[参数:毫秒][返回值:定时器整型编号]来开启一个定时器 ...

  2. SQL查询遍历数据方法一 [ 临时表 + While循环]

    以下以SQL Server 2000中的NorthWind数据库中的Customers表为例, 用 临时表 + While循环 的方法, 对Customers表中的CompanyName列进行遍历 c ...

  3. quartz开源作业调度框架的配置

    quartz开源作业调度框架的job服务实现,Quartz是一个完全由java编写的开源作业调度框架,使用时候需要创建一个实现org.quartz.Job接口的java类,Job接口包含唯一的方法: ...

  4. schema与catalog的理解

    sql环境中Catalog和Schema都属于抽象概念,主要用来解决命名冲突问题.一个数据库系统包含多个Catalog,每个Catalog包含多个Schema,每个Schema包含多个数据库对象(表. ...

  5. c++中类似于java jprofiler/eclispe memoryanalysis的性能以及内存分析工具

    visual studio有自带的,可以看MSDN,不过一般来说,我们比较关注linux下的,搜了下,比较好用的应该有gprof和valgrind,先记录,可参考如下: http://blog.csd ...

  6. 20145305 《网络对抗》Web基础

    实践过程及结果截图 Apache 简单的网页编写 javascript相关 PHP测试 MySQL基础 php+mysql 登录成功信息: 登录失败信息: SQL注入 XSS攻击 基础问题回答 (1) ...

  7. 20145312 《网络对抗》 Web安全基础实践

    20145312 <网络对抗> Web安全基础实践 问题回答 SQL注入攻击原理,如何防御 原理:攻击者在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,把SQL语句当做 ...

  8. Python3基础 A类作为B类的实例变量

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. 在ubuntu下随意编译安装需要的python版本

    一.环境 ubuntu14.04 二.准备 2.1更新软件库 sudo apt-get update 2.2安装编译器及相应工具 2.3安装相应的开发库 sudo apt-get install zl ...

  10. insert into 和 where not exists

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/3569bd60-1299-4fe4-bfa1-d77ffa3e579f/insert ...