Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard problems.
This is your first year at summer camp, and you are asked to solve the following problem. All integers starting with 1are written in one line. The prefix of these line is "123456789101112131415...". Your task is to print the n-th digit of this string (digits are numbered starting with 1.
The only line of the input contains a single integer n (1 ≤ n ≤ 1000) — the position of the digit you need to print.
Print the n-th digit of the line.
3
3
11
0
In the first sample the digit at position 3 is '3', as both integers 1 and 2 consist on one digit.
In the second sample, the digit at position 11 is '0', it belongs to the integer 10.
题意:就是一个数字串。问第n个位置是什么数字。是第n个位置,不是问第n个数。
思路:XJB模拟。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int MAXN=2000+5;
#define INF 0x3f3f3f3f
int digit[MAXN],tmp[MAXN];
void init(){
int cnt=1;
for(int i=1; cnt<=1000; i++){
int num=i, tmpcnt=0;
while(num){
tmp[++tmpcnt]=num%10;
num/=10;
}
while(tmpcnt>=1){
digit[cnt++]=tmp[tmpcnt--];
}
}
}
int main()
{
int n;
init();
while(scanf("%d",&n)!=EOF){
printf("%d\n",digit[n]);
}
return 0;
}
A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to be different.
Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string "aba" has substrings "" (empty substring), "a", "b", "a", "ab", "ba", "aba".
If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.
Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the length of the string s.
The second line contains the string s of length n consisting of only lowercase English letters.
If it's impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.
2 aa
1
4 koko
2
5 murat
0
In the first sample one of the possible solutions is to change the first character to 'b'.
In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".
题意:给定一个长度为n的字符串。 要求字符串不存在重复子串,每次可以修改一个位置的字符。 问至少要修改几次。 或者不存在修改方案输出-1
思路:题目意思就是不能出现相同字符。然后发现当长度大于26则不可能有满足的方案。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int MAXN=100000+5;
#define INF 0x3f3f3f3f
char str[MAXN];
int vis[26];
int main()
{
int n,ans;
while(scanf("%d",&n)!=EOF){
scanf("%s",str);
if(n>26){
printf("-1\n");
}
else{
ans=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++){
if(vis[str[i]-'a']){
ans++;
}
vis[str[i]-'a']=1;
}
printf("%d\n",ans);
}
}
return 0;
}
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.
We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position(xi, yi). Both Adil and Bera can carry only one bottle at once each.
For both Adil and Bera the process looks as follows:
- Choose to stop or to continue to collect bottles.
- If the choice was to continue then choose some bottle and walk towards it.
- Pick this bottle and walk to the recycling bin.
- Go to step 1.
Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.
They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.
First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.
The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.
Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle.
It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.
Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .
3 1 1 2 0 0 3 1 1 2 1 2 3
11.084259940083
5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3
33.121375178000
Consider the first sample.
Adil will use the following path: .
Bera will use the following path: .
Adil's path will be units long, while Bera's path will be
units long.
题意:给定两个人的起始坐标和箱子的坐标, 然后有n个要装进箱子的物品的坐标。要求要把这n个物品全部装进箱子里面。每次只能拿一个物品。把物品放到箱子后再去拿第二个物品。。。问最少需要走多长的距离。
思路:主要分2种情况。 1:只一个人去拿物品 2:两个人一起去拿物品。 然后判断下那种跟优即可。 主要影响最后结果的只有第一个物品的点。枚举2个人以每一个点为第一个物品的点。判断哪个人该去那个点能减少距离更多。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const double INF=1000000000000000.0;
const int MAXN=100000+5;
struct Point
{
double x,y;
}A,B,R,P[MAXN];
struct Node{
double dis;
int id;
Node(double a,int b):dis(a),id(b){};
};
bool cmp(Node a,Node b){
return a.dis>b.dis;
}
vector<Node>AP,BP;
double dist(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int n;
while(scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&R.x,&R.y)!=EOF){
double ans=0; AP.clear();BP.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf%lf",&P[i].x,&P[i].y);
double P_R=dist(P[i],R),A_P=dist(A,P[i]),B_P=dist(B,P[i]);
ans+=P_R*2; //每个物品的来回距离。
AP.push_back(Node(P_R-A_P,i)); //第一个人以每个点为第一个物品的距离
BP.push_back(Node(P_R-B_P,i)); //第二个人
}
sort(AP.begin(),AP.end(),cmp); //找到每个人的最优起点
sort(BP.begin(),BP.end(),cmp); //同上
//only A to pick
Node ma=AP[0]; double ansa=ans-dist(P[ma.id],R)+dist(P[ma.id],A);
//only B to pick
Node mb=BP[0]; double ansb=ans-dist(P[mb.id],R)+dist(P[mb.id],B);
//A and B pick together
double ansab=INF;
if(ma.id!=mb.id){
ansab=ans-dist(P[ma.id],R)+dist(P[ma.id],A)-dist(P[mb.id],R)+dist(P[mb.id],B);
}
else if(ma.id==mb.id&&n>1){
Node mma=AP[1]; Node mmb=BP[1];
ansab=min(ans-dist(P[ma.id],R)+dist(P[ma.id],A)-dist(P[mmb.id],R)+dist(P[mmb.id],B),
ans-dist(P[mma.id],R)+dist(P[mma.id],A)-dist(P[mb.id],R)+dist(P[mb.id],B));
}
printf("%lf\n",min(ansa,min(ansb,ansab)));
}
return 0;
}
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire ink days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.
The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.
Print a single line containing the difference between richest and poorest peoples wealth.
4 1 1 1 4 2
2
3 1 2 2 2
0
Lets look at how wealth changes through day in the first sample.
- [1, 1, 4, 2]
- [2, 1, 3, 2] or [1, 2, 3, 2]
So the answer is 3 - 1 = 2
In second sample wealth will remain the same for each person.
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int MAXN=500000+5;
typedef long long int LL;
#define INF 0x3f3f3f3f
int c[MAXN],n,k;
bool checkrich(LL x){
LL need=0;
for(int i=1;i<=n;i++){
need+=max(1LL*0,1LL*(c[i]-x));
}
if(need<=k){
return true;
}
return false;
}
bool checkpoor(LL x){
LL need=0;
for(int i=1;i<=n;i++){
need+=max(1LL*0,1LL*(x-c[i]));
}
if(need<=k){
return true;
}
return false;
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF){
int minc=INF,maxc=-1;
LL sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&c[i]);
sum+=c[i];
minc=min(minc,c[i]);
maxc=max(maxc,c[i]);
}
int l,r,mid,richest,poorest;
//get the richest
l=sum%n==0?sum/n:sum/n+1; r=INF; //最富有的人的钱至少为l
while(r>=l){
mid=(l+r)/2;
if(checkrich(mid)){
richest=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
//get the poorest
l=1; r=sum/n; //最穷的人拥有的钱最多为r
while(r>=l){
mid=(l+r)/2;
if(checkpoor(mid)){
poorest=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
printf("%d\n",richest-poorest);
}
return 0;
}