CodeForces ~ 996
Allen has a LOT of money. He has nn dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are 11, 55, 1010, 2020, 100100. What is the minimum number of bills Allen could receive after withdrawing his entire balance?
Input
The first and only line of input contains a single integer nn (1≤n≤1091≤n≤109).
Output
Output the minimum number of bills that Allen could receive.
Examples
125
3
43
5
1000000000
10000000
Note
In the first sample case, Allen can withdraw this with a 100100 dollar bill, a 2020dollar bill, and a 55 dollar bill. There is no way for Allen to receive 125125 dollars in one or two bills.
In the second sample case, Allen can withdraw two 2020 dollar bills and three 11 dollar bills.
In the third sample case, Allen can withdraw 100000000100000000 (ten million!) 100100 dollar bills.
解题思路:简单贪心
Allen wants to enter a fan zone that occupies a round square and has nn entrances.
There already is a queue of aiai people in front of the ii-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.
Allen uses the following strategy to enter the fan zone:
- Initially he stands in the end of the queue in front of the first entrance.
- Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance).
Determine the entrance through which Allen will finally enter the fan zone.
Input
The first line contains a single integer nn (2≤n≤1052≤n≤105) — the number of entrances.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the number of people in queues. These numbers do not include Allen.
Output
Print a single integer — the number of entrance that Allen will use.
Examples
4
2 3 2 0
3
2
10 10
1
6
5 2 6 5 7 4
6
Note
In the first example the number of people (not including Allen) changes as follows: [2,3,2,0]→[1,2,1,0]→[0,1,0,0][2,3,2,0]→[1,2,1,0]→[0,1,0,0]. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.
In the second example the number of people (not including Allen) changes as follows: [10,10]→[9,9]→[8,8]→[7,7]→[6,6]→[5,5]→[4,4]→[3,3]→[2,2]→[1,1]→[0,0][10,10]→[9,9]→[8,8]→[7,7]→[6,6]→[5,5]→[4,4]→[3,3]→[2, 2]→[1,1]→[0,0].
In the third example the number of people (not including Allen) changes as follows: [5,2,6,5,7,4]→[4,1,5,4,6,3]→[3,0,4,3,5,2]→[2,0,3,2,4,1]→[1,0,2,1,3,0]→[0,0,1,0,2,0][5,2,6,5,7,4]→[4,1,5,4,6,3]→[3,0,4,3,5,2]→[2,0,3,2,4,1]→[1,0,2,1,3,0]→[0,0,1,0,2,0].
题意:有n 个元素,而你会按顺序每个等一分钟,看哪个先到0,每过一分钟,都要减1
思路:你可以把每个元素算出一个步数,至少第几步才可以排到这个,首先看这个元素是否>n,大于的话说明还要走几圈,可以算出这个圈数再来算步数
这是我的方法,但是后来看了下大牛的方法,直接算出圈数,选最小的圈数的那个,有相同的圈数的话选最先出现的那个,这样方便了很多
引用大牛博客https://blog.csdn.net/ZscDst/article/details/80807353
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+;
const int INF = 0x3f3f3f3f;
int n, ans;
int main()
{
scanf("%d", &n);
int MIN = INF;
for (int i = ; i <= n; i++)
{
int a; scanf("%d", &a);
if(MIN > (a-i+n)/n)//x*n+i=a =》x=abs(a-i)/n
{
MIN = (a-i+n)/n;
ans = i;
}
}
printf("%d\n", ans);
return ;
}
Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.
Allen's future parking lot can be represented as a rectangle with 44 rows and nn (n≤50n≤50) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having kk (k≤2nk≤2n) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places.
Illustration to the first example.
However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space.
Allen knows he will be a very busy man, and will only have time to move cars at most 2000020000 times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.
Input
The first line of the input contains two space-separated integers nn and kk (1≤n≤501≤n≤50, 1≤k≤2n1≤k≤2n), representing the number of columns and the number of cars, respectively.
The next four lines will contain nn integers each between 00 and kk inclusive, representing the initial state of the parking lot. The rows are numbered 11 to 44 from top to bottom and the columns are numbered 11 to nn from left to right.
In the first and last line, an integer 1≤x≤k1≤x≤k represents a parking spot assigned to car xx (you can only move this car to this place), while the integer 00 represents a empty space (you can't move any car to this place).
In the second and third line, an integer 1≤x≤k1≤x≤k represents initial position of car xx, while the integer 00 represents an empty space (you can move any car to this place).
Each xx between 11 and kk appears exactly once in the second and third line, and exactly once in the first and fourth line.
Output
If there is a sequence of moves that brings all of the cars to their parking spaces, with at most 2000020000 car moves, then print mm, the number of moves, on the first line. On the following mm lines, print the moves (one move per line) in the format ii rr cc, which corresponds to Allen moving car ii to the neighboring space at row rr and column cc.
If it is not possible for Allen to move all the cars to the correct spaces with at most 2000020000 car moves, print a single line with the integer −1−1.
Examples
4 5
1 2 0 4
1 2 0 4
5 0 0 3
0 5 0 3
6
1 1 1
2 1 2
4 1 4
3 4 4
5 3 2
5 4 2
1 2
1
2
1
2
-1
1 2
1
1
2
2
2
1 1 1
2 4 1
Note
In the first sample test case, all cars are in front of their spots except car 55, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most 2000020000 will be accepted.
In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible.
引用大牛博客:https://blog.csdn.net/ZscDst/article/details/80821376
Allen is hosting a formal dinner party. 2n2n people come to the event in nn pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.
Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.
Input
The first line contains a single integer nn (1≤n≤1001≤n≤100), the number of pairs of people.
The second line contains 2n2n integers a1,a2,…,a2na1,a2,…,a2n. For each ii with 1≤i≤n1≤i≤n, iiappears exactly twice. If aj=ak=iaj=ak=i, that means that the jj-th and kk-th people in the line form a couple.
Output
Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.
Examples
4
1 1 2 3 3 2 4 4
2
3
1 1 2 2 3 3
0
3
3 1 2 3 1 2
3
Note
In the first sample case, we can transform 11233244→11232344→1122334411233244→11232344→11223344 in two steps. Note that the sequence 11233244→11323244→1133224411233244→11323244→11332244 also works in the same number of steps.
The second sample case already satisfies the constraints; therefore we need 00 swaps.
思路:简单贪心,只要遍历一遍,然后再找到当前元素交换过来即可
For a vector v⃗ =(x,y)v→=(x,y), define |v|=x2+y2−−−−−−√|v|=x2+y2.
Allen had a bit too much to drink at the bar, which is at the origin. There are nnvectors v1→,v2→,⋯,vn→v1→,v2→,⋯,vn→. Allen will make nn moves. As Allen's sense of direction is impaired, during the ii-th move he will either move in the direction vi→vi→ or −vi→−vi→. In other words, if his position is currently p=(x,y)p=(x,y), he will either move to p+vi→p+vi→ or p−vi→p−vi→.
Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position pp satisfies |p|≤1.5⋅106|p|≤1.5⋅106 so that he can stay safe.
Input
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of moves.
Each of the following lines contains two space-separated integers xixi and yiyi, meaning that vi→=(xi,yi)vi→=(xi,yi). We have that |vi|≤106|vi|≤106 for all ii.
Output
Output a single line containing nn integers c1,c2,⋯,cnc1,c2,⋯,cn, each of which is either 11 or −1−1. Your solution is correct if the value of p=∑ni=1civi→p=∑i=1ncivi→, satisfies |p|≤1.5⋅106|p|≤1.5⋅106.
It can be shown that a solution always exists under the given constraints.
Examples
3
999999 0
0 999999
999999 0
1 1 -1
1
-824590 246031
1
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
1 1 1 1 1 1 1 -1 思路:贪心+随机算法,(随机算法没听过,今天算是初次见面了)
大牛博客:https://blog.csdn.net/ZscDst/article/details/80818824
CodeForces ~ 996的更多相关文章
- 【Codeforces】Codeforces Round #492 (Div. 2) (Contest 996)
题目 传送门:QWQ A:A - Hit the Lottery 分析: 大水题 模拟 代码: #include <bits/stdc++.h> using namespace std; ...
- CodeForces 996B World Cup(思维)
https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- 用C#实现多种方式播放Wav声音
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- .Net调用QQ邮箱发送邮件
话说网上发送邮件的代码很多,但是我由于不细心,导致拿别人的代码发送邮件老是失败,今天就说说几个要注意的地方吧!!! public bool SendEmail() { MailMessage msg ...
- linux权限管理之基本权限
基本权限 UGO ======================================================== 文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件 ...
- 新项目的vue组件
项目地址:http://pan.baidu.com/s/1qYIxCXu 很久没有写博客的原因的是之前一直在解决一个问题,这个问题就是:我们在写组件的时候,官方推荐把css写在组件里面,但是如果我们写 ...
- Django中模型层中ORM的多表操作
ORM的多表创建(一对一.一对多,多对多): 1模型创建 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等 ...
- xShell的简单操作记录
查看java进程: ps ux | grep java 显示当前所有java进程pid的命令:jps 将压缩文件从本地上传到服务器上:rz 打包成jar包:mvn clean package -Dma ...
- leetcode-algorithms-20 Valid Parentheses
leetcode-algorithms-20 Valid Parentheses Given a string containing just the characters '(', ')', '{' ...
- poj-2888-矩阵+polya
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 6195 Accepted: 1969 D ...
- Beta阶段——第6篇 Scrum 冲刺博客
Beta阶段--第6篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 完成了函数的编写,提供报表数 ...
- JedisClusterMaxRedirectionsException: Too many Cluster redirections
发生环境 当时的redis集群配置:redis-trib.rb 127.0.0.1 .... redis.conf的bind是默认# 解决方案 将redis.conf的bind为局域网真实ip red ...