Time Limit: 1000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109.
At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.
Your task is to determine the k-th identifier to be pronounced.
Input
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·109, n·(n + 1) / 2).
The second line contains the sequence id1, id2, ..., idn (1 ≤ idi ≤ 109) — identifiers of roborts. It is guaranteed that all identifiers are different.
Output
Print the k-th pronounced identifier (assume that the numeration starts from 1).
Sample Input
2 2 1 2
1
4 5 10 4 18 3
4
Source
Codeforces Round #350 (Div. 2)
思路就是等差数列前n项和:注意要强制转换longlong
#include<bits/stdc++.h>
using namespace std;
int a1[100000+10];
int main()
{
int n;long long k;
scanf("%d%lld",&n,&k);
for(int i=0;i<n;i++) scanf("%d",&a1[i]);
long long sum=0;
int flag=0;
for(int i=1;i<=n;i++){
sum=(long long)(i+1)*i/2;
if(sum>k) { flag=i; break; }
else if(sum==k) { printf("%d\n",a1[i-1]); return 0; }
}
long long a=flag*(flag+1)/2;
long long b=(long long)flag*(flag-1)/2;
printf("%d\n",a1[k-b-1]);
return 0;
}
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
用一个一维dp数组,记录到i项的最大值,当和为负数时,从0开始加。
if(d[i-1]<0) d[i]=a[i];
else d[i]=d[i-1]+a[i];
#include<bits/stdc++.h>
using namespace std;
#define N 100010
int a[N],d[N];
int main()
{
int t,k=1;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
d[0]=a[0];
for(int i=1;i<n;i++){
if(d[i-1]<0) d[i]=a[i];
else d[i]=d[i-1]+a[i];
}
int ma=d[0],e=0;
int mx=ma;
for(int i=1;i<n;i++){
if(d[i]>ma) { ma=d[i],e=i; }
}
int q=e,o=0;
for(int i=e;i>=0;i--)
{
o+=a[i];
if(ma==o) { q=i;}
}
cout<<"Case "<<k++<<":"<<endl<<ma<<" "<<q+1<<" "<<e+1<<endl;
if(t) cout<<endl;/*
printf("Case %d:\n%d %d %d\n",k++,ma,q+1,e+1);
if(t) printf("\n");*/
}
return 0;
}
Time Limit: 3000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.
Given the list of strings, output the lexicographically smallest concatenation.
Input
The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104).
Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.
Output
Print the only string a — the lexicographically smallest string concatenation.
Sample Input
4 abba abacaba bcd er
abacabaabbabcder
5 x xx xxa xxaa xxaaa
xxaaaxxaaxxaxxx
3 c cb cba
cbacbc
Source
cf题,思路挺好,改进排序的方法。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
bool cmp(string a,string b){
return a+b<b+a;
}
int main()
{
string a[50000+1];
int n;scanf("%d",&n);
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n,cmp);
for(int i=0;i<n;i++){
cout<<a[i];
}
return 0;
}
一天做一道题就好,反正现在也没那么功利了,完全靠兴趣。
真正要功利的是应试考研,方法总是有的,但要真的努力与动力。
有时候无论作什么都没那么多理由,但你心中总要有一个信念,而不要掺杂太多思想。
那就是成为大人物的渴望,不再那么渺小吧。梦想有一天真正成为自己想成为的那个人,现在就是不懈地区努力。
未来是什么样真的不知道,但毕竟充满危险压力与残酷,武装好自己,做好自己才是真正现在应该做的事。