代码:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAX = 1e6+10;
const int INF = 0x3fffffff;
char s[MAX];
int a[MAX];
int b[MAX];
struct node{
int left;
int right;
}p[MAX];
int main(){
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t;
scanf("%d",&t);
while(t--){
memset(s,0,sizeof(s));
scanf("%s",s);
getchar();
int n = strlen(s);
int num=0;
int ans=0;
for(int i=0;i<n;i++){
if(s[i]=='Q') num++;
a[i]=num;
if(s[i]=='A') p[ans++].left=a[i];
}
int temp = ans;
num=0;
for(int i=n-1;i>=0;i--){
if(s[i]=='Q') num++;
b[n-i-1]=num;
if(s[i]=='A') p[--temp].right=b[n-i-1];
}
LL final=0;
for(int i=0;i<ans;i++)
final += p[i].left*p[i].right;
printf("%lld\n",final);
}
return 0;
}
代码:
#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100005];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,k;
while(cin>>n>>k)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b,b+n);
if(k==1)
printf("%d\n",b[0]);
else if(k==2)
{
int p=max(a[0],a[n-1]);
printf("%d\n",p);
}
else
printf("%d\n",b[n-1]);
}
return 0;
}
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
struct node{
char name[25];
char val[1005];
int id;
int cnt;
int ck;
}a[105],b[105];
bool cmp(node a,node b){
if (a.ck == b.ck) return strcmp(a.name,b.name)<0;
else return a.ck > b.ck;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while (T--){
int k,l,m;
scanf("%d%d%d",&k,&l,&m);
for (int i = 0;i < m; ++i){
scanf("%s%s%d",a[i].name,a[i].val,&a[i].id);
}
int o = 0;
for (int i = 0;i < m; ++i){
int len = strlen(a[i].val);
int N,H,K;
N = H = K = 0;
for (int j = 0; j < len; ++j){
if (a[i].val[j] == 'N') N++;
if (a[i].val[j] == 'H') H++;
if (a[i].val[j] == 'K') K++;
}
a[i].cnt = min(N,min(H,K)); //NHK出现次数
if (a[i].cnt > k && a[i].id < l){ //P>K 且 Q < L
b[o].cnt = a[i].cnt;
b[o].id = a[i].id;
strcpy(b[o].name,a[i].name);
strcpy(b[o].val,a[i].val);
b[o].ck = b[o].cnt*(l-b[o].id);//P*(L-Q)
o++;
}
}
if (o == 0) printf("FINE!\n");
else{
sort(b,b+o,cmp);
for (int i = 0;i < o; ++i){
printf("%s\n",b[i].name);
}
}
}
return 0;
}
代码:
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
const int MAXN = 1e5+10;
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--) {
int n, x;
cin >> n;
map<int, int> mp;
for(int i = 1; i <= n; ++i) {
cin >> x;
mp[x]++;
}
int ans = 0;
for(int i = 1; i <= n; ++i) {
if(mp[i] >= 2) {
ans += mp[i]/2;
mp[i] %= 2;
}
if(mp[i] && mp[i+1]&1 && mp[i+2]) {
ans++;
mp[i]--, mp[i+1]--, mp[i+2]--;
}
}
cout << ans << endl;
}
return 0;
}
代码:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("in.txt", "w", stdout);
int a, b;
bool flag = false;
while(scanf("%d%d", &a, &b) != EOF) {
if(flag) puts("");
else flag = true;
int d = gcd(a, b);
printf("%lld", 1LL * a * b / d / d);
}
return 0;
}
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
LL a[MAXN];
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n, T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i < n; ++i) scanf("%lld", &a[i]);
if(n == 1) puts("YES 1");
else if(n == 2) {
if(a[1] == 1) puts("YES 2");
else puts("YES 1");
}
else if(n == 3) {
LL cnt = -1;
LL ans = a[2] - a[1];
if(ans > 0) {
if(a[1] - ans > 0 && a[1] - ans != ans) cnt = a[1] - ans;
else if(ans % 2 == 0 && a[1] - ans / 2 != 0) cnt = a[1] + ans / 2;
else if(a[1] - ans != 0) cnt = a[2] + ans;
}
else if(ans < 0) {
if(a[2] + ans > 0) cnt = a[2] + ans;
else if(ans % 2 == 0) cnt = a[1] + ans / 2;
else cnt = a[1] - ans;
}
if(cnt == -1) puts("NO");
else printf("YES %lld\n", cnt);
}
}
return 0;
}
代码:
#include<cstdio>
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int m,n;
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&n);
if(m>n){
int t=m;
m=n;
n=t;
}
if(n<=2*m){
if((m+n)%3==0){
printf("Yes\n");
}else{
printf("No\n");
}
}else{
printf("No\n");
}
}
return 0;
}