B - Element Swapping
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=100010;
int n,m,k;
ll sum[maxn],x,y,X,Y;
ll ans,ct,cnt,tmp,flag;
struct node
{
ll v,id;
bool operator<(node aa)const
{
return v<aa.v;
}
}a[maxn];
int ok[maxn];
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--){
scanf("%d%lld%lld",&n,&X,&Y);
x=X;y=Y;
rep(i,1,n)
{
ok[i]=0;
scanf("%lld",&a[i].v);
x-=(ll)i*a[i].v;
y-=(ll)i*a[i].v*a[i].v;
a[i].id=i;
}
sort(a+1,a+n+1);
ans=0;
if(x==0&&y) {puts("0");continue;}
if(x==0&&y==0)
{
int i=1;
ll tmp=0;
while(i<=n)
{
tmp=0;
int j=i;
while(j<=n&&a[j].v==a[i].v) {j++;tmp++;}
ans+=(tmp*(tmp-1))/2;
i=j;
}
printf("%lld\n",ans);
continue;
}
else if(y%x) {puts("0");continue;}
else {
int tim=1;
y=y/x;
int i=1,j=n;
while(i<j)
{
tim++;
int k=i;
ll tmp=0;
ll cnt=0;
while(k<j&&2LL*a[k].v==y&&a[k].v==a[i].v)
k++;
if(i!=k) {i=k;continue;}
tmp=0;
while(j>i&&a[j].v+a[i].v>y) j--;
if(j==i) break;
if(a[i].v+a[j].v!=y) {i++;continue;}
if(x%(a[j].v-a[i].v)!=0) {i++;continue;}
ll z=x/(a[j].v-a[i].v);
//cout<<x<<" "<<a[j].v-a[i].v<<" "<<z<<endl;
k=i;
while(k<j&&a[k].v==a[i].v) {
if((ll)a[k].id-z<=(ll)n&&(ll)a[k].id-z>0LL)
{
ok[a[k].id-(int)z]=tim;
//cout<<a[k].id<<" "<<z<<endl;
}
k++;
}
while(j>=k&&a[i].v+a[j].v==y) {
if(ok[a[j].id]==tim) ans++;
j--;
}
i=k;
}
}
printf("%lld\n",ans);
// if(flag) puts("Yes"); else puts("No");
}
return 0;
}
代码转自:https://blog.csdn.net/lsd20164388/article/details/89606289
C - Array in the Pocket
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
const int maxn = 1e5 + 10;
int a[maxn], b[maxn], c[maxn], cnt[maxn];
int n;
struct node{
int v, pos;
}p[maxn];
typedef pair<int, int> P;
int main(){
int T;
scanf("%d", &T);
while(T--){
clr(a); clr(b); clr(c); clr(cnt);
scanf("%d", &n);
set<P> s, se;
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
cnt[a[i]]++;
c[a[i]] += 2;
}
for(int i = 1; i <= n; i++){
if(cnt[i]){
s.insert(P(c[i], i));
se.insert(P(i, cnt[i]));
}
}
if((--s.end())->first > n){
printf("Impossible\n");
continue;
}
for(int i = 1; i <= n; i++){
s.erase(P(c[a[i]], a[i]));
s.insert(P(--c[a[i]], a[i]));
P mx = *(--s.end());
if(mx.first == n-i+1){
b[i] = mx.second;
}
else{
if(a[i] == (se.begin())->first)
b[i] = (++se.begin())->first;
else
b[i] = (se.begin())->first;
}
se.erase(P(b[i], cnt[b[i]]));
if(--cnt[b[i]]) se.insert(P(b[i], cnt[b[i]]));
s.erase(P(c[b[i]], b[i]));
if(--c[b[i]]) s.insert(P(c[b[i]], b[i]));
}
for(int i = 1; i <= n; i++)
printf("%d%c", b[i], i == n ? '\n' : ' ');
}
return 0;
}
E - Sequence in the Pocket
#include<bits/stdc++.h>
using namespace std;
#define line cout<<"----------"<<endl
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int T, n;
int main(){
scanf("%d", &T);
while(T--){
scanf("%d", &n);
int a[N], b[N];
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b+1, b+n+1);
int cnt = 0;
for(int i = n; i >= 1; i--) {
if(a[i] == b[i+cnt]) continue;
else cnt++;
}
printf("%d\n", cnt);
}
return 0;
}
F - Abbreviation
#include<bits/stdc++.h>
using namespace std;
#define line cout<<"----------"<<endl
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int T, n;
int main(){
scanf("%d", &T);
getchar();
while(T--){
char s[maxn];
gets(s);
int len = strlen(s);
printf("%c", s[0]);
for(int i = 1; i < len; i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='y')
continue;
else printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
G - Lucky 7 in the Pocket
#include<bits/stdc++.h>
using namespace std;
#define line cout<<"----------"<<endl
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int T, n;
int main(){
scanf("%d", &T);
while(T--){
scanf("%d", &n);
int x = n % 7;
if(n % 7 == 0 && n % 4 != 0){
printf("%d\n", n);
continue;
}
else {
n += (7-x);
while(n % 7 == 0 && n % 4 == 0){
n += 7;
break;
}
printf("%d\n", n);
}
}
return 0;
}
H - Singing Everywhere
#include<bits/stdc++.h>
using namespace std;
#define line cout<<"----------"<<endl
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int T, n;
int a[maxn];
int main(){
scanf("%d", &T);
getchar();
while(T--){
memset(a, INF, sizeof(a));
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int cnt = 0;
int maxx = 0;
for(int i = 2; i < n; i++){
if(a[i] > a[i-1] && a[i] > a[i+1]) {
cnt++;
if((i+1 == n) || (i-1 == 1)) maxx = max(maxx, 1);
if(a[i-1] <= a[i+1]) maxx = max(maxx, 1);
if(a[i+1] < a[i+2] && a[i+2] > a[i+3]){
if(a[i] == a[i+2]) maxx = max(maxx, 2);
else maxx = max(maxx, 1);
}
}
}
printf("%d\n", cnt-maxx);
}
return 0;
}
I - Fibonacci in the Pocket
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int T = cin.nextInt();
String str = cin.nextLine();
while(T-- > 0){
BigInteger p = BigInteger.valueOf(0);
BigInteger q = BigInteger.valueOf(1);
BigInteger r = BigInteger.valueOf(2);
BigInteger s = BigInteger.valueOf(3);
BigInteger l = cin.nextBigInteger();
BigInteger x = cin.nextBigInteger();
BigInteger a = l.mod(s);
BigInteger b = x.mod(s);
if(a.equals(b)){
if(a.equals(q) || a.equals(r)) System.out.println("1");
else System.out.println("0");
}
else{
if(a.equals(p)){
if (b.equals(q)) System.out.println("1");
else if(b.equals(r))System.out.println("0");
}
else if(a.equals(q)){
if (b.equals(p)) System.out.println("0");
else if(b.equals(r))System.out.println("0");
}
else if(a.equals(r)){
if (b.equals(p)) System.out.println("1");
else if(b.equals(q))System.out.println("0");
}
}
}
}
}
J - Welcome Party
#include<bits/stdc++.h>
using namespace std;
#define line cout<<"----------"<<endl
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 2e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int f[MAXN],n,m;
int Find(int x) {
if(f[x] == x) return x;
return f[x] = Find(f[x]);
}
void mix(int x,int y) {
int dx = Find(x);
int dy = Find(y);
if(dx > dy) f[dx] = dy;
else f[dy] = dx;
}
int ans[MAXN],tot,vis[MAXN];
vector<int> ve[MAXN];
void bfs(int st) {
priority_queue<int, vector<int>, greater<int> > pq;
pq.push(st);
while(!pq.empty()) {
int cur = pq.top();
pq.pop();
if(vis[cur] == 1) continue;
vis[cur] = 1;
ans[++tot] = cur;
int up = ve[cur].size();
for(int i = 0; i < up; i++) {
int v = ve[cur][i];
if(vis[v]) continue;
pq.push(v);
}
}
}
int main(){
int T;
scanf("%d", &T);
while(T--) {
tot = 0;
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; i++) {
f[i] = i;
ve[i].clear();
vis[i] = 0;
}
for(int u,v,i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
ve[u].push_back(v);
ve[v].push_back(u);
mix(u, v);
}
int ans1 = 0;
for(int i = 1; i <= n; i++) {
if(f[i] == i) {
ve[0].push_back(i);
ans1++;
}
}
bfs(0);
printf("%d\n", ans1);
for(int i = 2; i <= tot; i++) {
printf("%d%c", ans[i], i == tot ? '\n' : ' ');
}
}
return 0 ;
}
K - Strings in the Pocket
#include<bits/stdc++.h>
using namespace std;
#define line cout<<"----------"<<endl
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
const int maxn = 2e5 + 10;
const int MAXN = 2e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
char a[MAXN], b[MAXN];
int n, odd[MAXN], eve[MAXN], T;
ll manachar(){
int l = 0, r = 0,x;
ll ans = 0;
for(int i = 1; i <= n; i++){
if(i > r) x = 1;
else x = min(odd[l+r-i], r-i);
while(i-x>=1 && i+x<=n && a[i-x]==a[i+x])
x++;
odd[i] = x;
ans += x;
if(i+x-1 > r){
r = i+x-1;
l = i-x+1;
}
}
l = r = 0;
for(int i = 1; i <= n; i++){
if(i > r) x = 0;
else x = min(eve[l+r-i+1], r-i+1);
while(i-x-1>=1 && i+x<=n && a[i-x-1]==a[i+x])
x++;
eve[i] = x;
ans += x;
if(i+x >= r){
l = i-x;
r = i+x-1;
}
}
return ans;
}
int main(){
scanf("%d", &T);
while(T--){
scanf("%s", a+1);
scanf("%s", b+1);
n = strlen(a + 1);
int L = 1, R = n;
for(; L <= n; L++)
if(a[L] != b[L])
break;
for(; R >= 1; R--)
if(a[R] != b[R])
break;
if(L == R){
printf("0\n");
continue;
}
else if(L <= n){
int ans = 1;
for(int i = L; i <= R; i++){
if(a[i] != b[L+R-i]){
ans = 0;
break;
}
}
if(ans){
L--; R++;
while(L>=1 && R<=n && a[L]==b[R] && a[R]==b[L]){
ans++;L--;R++;
}
}
printf("%d\n", ans);
}
else printf("%lld\n",manachar());
}
}