Bilibili
1.
Given an array, these numbers can be connected together to form a large number, and find the largest number.
For example, the maximum number that [3, 30, 34, 5, 9] can form is 9534330.
Since the number of components may be very large, use a string to return.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<climits>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#include<sstream>
using namespace std;
bool cmpstr(string a, string b){
if( a+b>b+a)
return true;
else
return false;
}
string largestNumber(vector<int>& nums) {
vector<string> t;
string res = "";
for(int i = 0 ; i < nums.size(); i ++)
t.push_back(std::to_string(nums[i]));
for(int i = 0 ; i < t.size()-1; i ++)
{
for(int j = i+1 ; j < t.size() ; j++)
{
if(!cmpstr(t[i],t[j]))
swap(t[i],t[j]);
}
}
for(int i = 0 ; i < nums.size(); i++)
res+= t[i];
int p = 0;
while(res[p] == '0')
p++;
if(p == res.length())
return "0";
else
return res.substr(p);
}
vector<int> ToIntegerVector(string input) {
vector<int> output;
stringstream ss;
ss.str(input);
string item;
char delim = ',';
while (getline(ss, item, delim)) {
output.push_back(std::stoi(item));
}
return output;
}
int main(){
string ss;
cin>> ss;
vector<int> input = ToIntegerVector(ss);
string str = largestNumber(input);
cout<<str<<endl;
return 0;
}
from functools import cmp_to_key
aa = list(map(str, input().strip().split(',')))
def cmp1(a,b):
if a+b>b+a:
return 1
if a+b<b+a:
return -1
else:
return 0
return cmp(a+b,b+a)
aa.sort(key=cmp_to_key(cmp1),reverse=True)
print(''.join(aa))
2.
Given an array nums and a positive integer s, find the length of the sub-array with the smallest length greater than s among the sub-arrays of nums.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#include<sstream>
using namespace std;
int minSubArrayLen(int s, vector<int>& nums) {
int ans = INT_MAX;
int i = 0;
int sum = 0;
int begin = 0;
while(i < nums.size())
{
if(sum + nums[i] < s)
{
sum += nums[i];
++ i;
}
else
{
if(i - begin < ans)
ans = i - begin + 1;
sum = sum - nums[begin];
++ begin;
}
}
return ans == INT_MAX? -1:ans;
}
int main(){
int n,s;
scanf("%d %d",&n,&s);
vector<int> input;
int t;
for(int i=0;i<n;i++){
scanf("%d",&t);
input.push_back(t);
}
int ans=minSubArrayLen(s,input);
cout<<ans<<endl;
return 0;
}
3.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#include<sstream>
using namespace std;
int p[100001];
int res[100001];
int ans1=0,ans2=0;
void find(int x,int d){
res[x]=res[x]+d+1;
if(p[x]==x){
if(res[x]>ans1){
ans1=res[x];
ans2=x;
}
return;
}
else{
find(p[x],d);
return;
}
}
int main(){
int n;
scanf("%d",&n);
int t1,t2;
memset(res,0,sizeof(res));
for(int i=0;i<100001;i++){
p[i]=i;
}
for(int i=0;i<n;i++){
scanf("%d %d",&t1,&t2);
p[t2]=t1;
find(t1,res[t2]);
//cout<<res[t1]<<endl;
}
cout<<ans2<<endl;
//system("PAUSE");
return 0;
}