`#include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
const int N = 100010;
typedef long long LL;
using namespace std;
LL w[N];
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++) cin >> w[i];
//排序将a、b分成两组 从数组中所取 可理解为左侧a的最大 右侧为b的最小
sort(w, w + n);
LL res = 1e9 + 10;
for(int i = 1; i <= n; i ++)
{
res = min(res, abs(w[i] - w[i - 1]));
}
printf("%ld", res);
return 0;
} `
`//小根堆#include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 100010;
typedef long long LL;
using namespace std;
LL w[N];
int main(){
int n;
cin >> n;
//priority_queue<数据类型, 容器类型, greater构造成小根堆> pq;
priority_queue<int, vector<int>, greater<int>> pq;
int t = -1;
for(int i = 0; i < n; i ++)
{
cin >> t;
pq.push(t);
}
LL res = 0;
while(pq.size() > 1)
{
int sum1 = pq.top();
pq.pop();
int sum2 = pq.top();
pq.pop();
int sum3 = sum1 + sum2;
pq.push(sum3);
res += sum3;
}
cout << res;
//printf("%ld", res);
return 0;
} `
////暴力实现
`#include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 30010;
typedef long long LL;
using namespace std;
int main(){
int w;
int p[N];
cin >> w;
int n;
cin >> n;
for(int i = 0; i < n; i ++) cin >> p[i];
//排序 不排序不满足从大开始实现
sort(p, p + n);
for(int i = n - 1; i >= 0; i --)
{
int flag = 0;
if(p[i] == -1) continue;
for(int j = i - 1; j >= 0; j --)
{
if(p[j] == -1) continue;
if(p[i] + p[j] <= w)
{
p[i] = -1;
p[j] = -1;
flag = 1;
break;
}
}
}
int sum = 0;
int sum1 = 0;
for(int i = 0; i < n; i ++)
{
if(p[i] < 0) sum1++;
else sum++;
}
cout << sum + sum1 / 2;
return 0;
}
////双指针
include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 30010;
typedef long long LL;
using namespace std;
int main(){
int w;
int p[N];
cin >> w;
int n;
cin >> n;
for(int i = 0; i < n; i ++) cin >> p[i];
sort(p, p + n);
int sum = 0;
int l = 0, r = n - 1;
while(l <= r)
{
if(p[l] + p[r] <= w)
{
l ++; r --;
sum ++;
}
else
{
r --;
sum ++;
}
}
cout << sum;
return 0;
}
`
//双指针
`#include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 30010;
typedef long long LL;
using namespace std;
int main(){
string s;
cin >> s;
int l = 0, r = s.length() - 1;
int flag = 1;
while(l <= r)
{
if(s[l] != s[r])
{
flag = 0;
break;
}
else
{
l ++; r --;
}
//if(l == r) break;
}
if(flag) cout << "Y";
else cout << "N";
return 0;
} `
`#include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 1e5 + 10;
typedef long long LL;
using namespace std;
int main(){
int n, s;
cin >> n >> s;
int a[N], b[N];
int len = 1e5 + 10;
int ans = len;
for(int i = 1; i <= n; i ++) cin >> a[i];
b[0] = 0;
for(int i = 1; i <= n; i ++) b[i] = b[i - 1] + a[i];
int j = 1;
for(int i = 1; i <= n; i ++)
{
if(j < i) j = i;
while(b[j] - b[i - 1] < s && j <= n)
{
j ++;
}
if(j <= n) ans = min(ans, j - i + 1);
}
if(ans == len) cout << 0;
else cout << ans << endl;
return 0;
} `
`//二进制求1的个数
#include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 1e5 + 10;
typedef long long LL;
using namespace std;
//计算最右边位于1的位置 模板适用
int lowbits(int x)
{
return x & (-x);
}
int main(){
int n;
cin >> n;
int b = 1;
LL cnt = 0;
while(n)
{
n -= lowbits(n);
cnt ++;
}
cout << cnt;
return 0;
} `
`#include[HTML_REMOVED]
//#include [HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
const int N = 1e4 + 10;
typedef long long LL;
using namespace std;
int lowbits(int x)
{
return x & (-x);
}
///////如何正确的判断该数是否为完全平方数
//根据数论知识:一个正整数因数个数为奇数当且仅当它是完全平方数(0 的情况需特殊处理),因此因数个数为偶数通常等价于该数不是一个完全平方数(题目可能对 0 也有规定)。
int is(int a)
{
int r = (int)(sqrt(a) + 1e-9);
return (r * r == a);
}
int a[N];
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
int cnt = 0;
for(int i = 1; i <= n; i ++)
{
int t = 0;
for(int j = i; j <= n; j ++)
{
//cout << t << " ";
t ^= a[j];
if(!is(t) && t != 0) cnt ++;
//cout << t << " ";
}
}
cout << cnt;
return 0;
} `