头像

somebutton01


访客:3309

离线


活动打卡代码 AcWing 136. 邻值查找

somebutton01
11个月前

include[HTML_REMOVED]

include[HTML_REMOVED]//STL set 动态维护有序序列

include[HTML_REMOVED]

include[HTML_REMOVED]

using namespace std;

const int N=1e5+10;
int a[N];

int main()
{
int n;
cin>>n;
set[HTML_REMOVED]> A;//同时存值和下标
A.insert({INT_MIN,0});//避免处理边界问题
A.insert({INT_MAX,0});
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
if(i>1)
{
auto it=A.upper_bound({a,0});//返回大于a的最小值的定位
auto lt=it;
lt–;
long long left=(long long) lt->first-a;
long long right=(long long) it->first-a;
if(abs(left)<=abs(right))//可以先存一下
{
cout<[HTML_REMOVED]second<[HTML_REMOVED]second<<endl;
}

    A.insert({a,i});
}
return 0;

}



活动打卡代码 AcWing 136. 邻值查找

somebutton01
11个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


活动打卡代码 AcWing 136. 邻值查找

somebutton01
11个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



somebutton01
11个月前

include[HTML_REMOVED]

include[HTML_REMOVED]//STL set

include[HTML_REMOVED]

include[HTML_REMOVED]

using namespace std;

const int N=1e5+10;
int a[N];

int main()
{
int n;
cin>>n;
set[HTML_REMOVED]> A;
A.insert({INT_MIN,0});
A.insert({INT_MAX,0});
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
if(i>1)
{
auto it=A.upper_bound({a,0});
auto lt=it;
lt–;
long long left=(long long) lt->first-a;
long long right=(long long) it->first-a;
if(abs(left)<=abs(right))
{
cout<[HTML_REMOVED]second<[HTML_REMOVED]second<<endl;
}

    A.insert({a,i});
}
return 0;

}



活动打卡代码 AcWing 136. 邻值查找

somebutton01
11个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


活动打卡代码 AcWing 51. 数字排列

somebutton01
11个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~



somebutton01
11个月前

class Solution {
public:
vector[HTML_REMOVED] st;
vector[HTML_REMOVED] path;
vector[HTML_REMOVED]> ans;

vector<vector<int>> permutation(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    st = vector<bool>(nums.size(), false);
    path = vector<int>(nums.size());
    dfs(nums, 0, 0);
    return ans;
}

void dfs(vector<int>& nums, int u, int start)
{
    if (u == nums.size())
    {
        ans.push_back(path);
        return;
    }

    for (int i = start; i < nums.size(); i ++ )
        if (!st[i])
        {
            st[i] = true;
            path[i] = nums[u];
            if (u + 1 < nums.size() && nums[u + 1] != nums[u])
                dfs(nums, u + 1, 0);
            else
                dfs(nums, u + 1, i + 1);
            st[i] = false;
        }
}

};




somebutton01
11个月前

class Solution {
public:

vector<int> findNumbersWithSum(vector<int>& nums, int target) {
    sort(nums.begin(),nums.end());
    for(int i = 0 ,j = nums.size() - 1; i <j;){
        if(nums[i] +nums[j] == target)
           return  vector<int>{nums[i],nums[j]};
        else if(nums[i] + nums[j] < target)
            i++;
        else 
            j--;
    }
}

};




somebutton01
11个月前

class Solution {
public:

vector<int> findNumbersWithSum(vector<int>& nums, int target) {
    sort(nums.begin(),nums.end());
    for(int i = 0 ,j = nums.size() - 1; i <j;){
        if(nums[i] +nums[j] == target)
           return  vector<int>{nums[i],nums[j]};
        else if(nums[i] + nums[j] < target)
            i++;
        else 
            j--;
    }
}

};



活动打卡代码 AcWing 53. 最小的k个数

somebutton01
11个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~