A

代码

using namespace std;
int t,n,x,i,j,tot;
int main()
{
string s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cin>>s;
tot=x=i=0;
bool flag=0;
while(s[i]=='0') i++;
for(;i<n;i++)
{
if(s[i]=='2' || s[i]=='3')
{
tot++;
continue;
}
if(flag==0 && s[i]=='1') flag=1;
if(flag==1 && s[i]=='1') x++;
if(flag==1 && s[i]=='0' && x) tot++,x--;
}
if(x>=2) tot+=x/2;
printf("%d\n",tot);
}
return 0;
}



B

代码

#include<iostream>
#include<cmath>
#include<stack>
#include<vector>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long ll;
const ll inf =1e17;
#define scn(a) scanf("%d",&a)
#define scd(a) scanf("%lf",&a)
#define scc(a) scanf("%c",&a)
#define scl(a) scanf("%lld",&a)
#define ptf(a) printf("%d\n",a)
#define mes(a,b) memset(a,b,sizeof(a))
#define fon(s,n) for(int i=s;i<=n;i++)
#define range(i,a,b) for(int i=a;i<=b;++i)
#define rerange(i,a,b) for(int i=a;i>=b;--i)
//#define N 100010
const int S=20;
ll gcd(ll a,ll b) { return b>0 ? gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll q_pow(ll a,ll b,ll mod)
{
ll ans=1,res=a;
while(b){
if(b&1) ans=ans*res%mod;
res=res*res%mod;
b>>=1;
}
return ans%mod;
}

int main() {

std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);

string s;
cin>>s;
string temp;
int q,pos;char c;
cin>>q;
int n=s.length();
while(q--){
cin>>c;
if(c=='M'){
cin>>pos;
}
else{
cin>>pos;
//pos=(pos+n)%n;
}
}
return 0;
}


C

代码

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false),cin.tie(0);
#define ll long long
#define inf 0x3f3f3f3f
const int N=1e5+5;
//set<string>b;
//set<string>::iterator it;
double eps=1e-4;
struct node
{
double x,y;
}p[25];
double dis(node a,node b)
{
return 1.0*sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cross(node a,node b,node c)
{
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int main()
{
IO;
int T,flag,i;
cin>>T;
while(T--)
{
flag=0;
node a,b,c;
for(i=0;i<20;i++)
{
cin>>p[i].x>>p[i].y;
}
for(i=0;i<20;i++)
{
if(fabs(dis(p[i],p[(i+1)%20])-9.0)<eps)
{
break;
}
}
a=p[i],b=p[(i+1)%20],c=p[(i+2)%20];
if(cross(a,b,c)>0&&fabs(dis(b,c)-6.0)<eps) flag=1;
if(cross(a,b,c)<0&&fabs(dis(b,c)-8.0)<eps) flag=1;
if(flag==1) cout<<"left\n";
else cout<<"right\n";
}
return 0;
}



E

思路

Ps：这个题答案到了ll，习惯性inf设0x3f3f3f3f会wa。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5;
const ll inf=1e18;
int a[maxn];
ll dp[maxn];
int main(){
int te;
cin>>te;
while(te--){
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i];
sort(a+1,a+1+n);
ll ans=0;
for(int i=2;i<=n;i+=2)
ans+=(a[i]-a[i-1]);
for(int i=1;i<=n;++i)
dp[i]=inf;
dp[4]=a[4]+a[3]-a[2]-a[1];
dp[6]=a[6]-a[4]+a[5]-a[2]+a[3]-a[1];
for(int i=8;i<=n;i+=2){
ll x,y;
x=dp[i-4]+a[i]+a[i-1]-a[i-2]-a[i-3];
y=dp[i-6]+a[i]-a[i-2]+a[i-1]-a[i-4]+a[i-3]-a[i-5];
dp[i]=min(x,y);
}
printf("%lld\n",ans+dp[n]);
}
return 0;
}



F

思路

b 拆分成 d * f， 所以c * f - d * e = a，这里就可以用扩展欧几里得求解。

代码

#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define vi vector<int>
#define SZ(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N = 2000005;
bool notp[N];
ll prime[N], pnum;
void sieve() {
memset(notp, 0, sizeof(notp));
notp[0] = notp[1] = 1;
pnum = 0;
for(int i = 2; i < N; i++) {
if(!notp[i]) prime[++pnum] = i;
for(int j = 1; j <= pnum && prime[j] * i < N; j++) {
notp[i *prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
}
void exgcd(ll a,ll b,ll &g,ll &x,ll &y) {//ax+by=gcd(a,b)=g
if (b == 0) {
g = a;
x = 1;
y = 0;
return;
}
exgcd(b, a % b, g, y, x);
y -= (a / b) * x;
}
int main() {
sieve();
int t;
scanf("%d", &t);
while(t--) {
ll a, b, c, d, e, f;
scanf("%lld%lld", &a, &b);
if(__gcd(a,b) != 1) {
ll gcd = __gcd(a,b);
printf("%lld %lld %lld %lld\n", a / gcd + 1, b / gcd, 1, b / gcd);
continue;
}
if(b == 1 || notp[b] == 0) {
printf("-1 -1 -1 -1\n");
continue;
}
ll tmp = b;
d = 1;
for(int i = 1; i <= pnum; i++) {
if(tmp == 1) break;
if(tmp % prime[i] == 0) {
while(tmp % prime[i] == 0) {
tmp /= prime[i];
d *= prime[i];
}
break;
}
}
f = tmp;
//      cout << f << " " << d << '\n';
if(tmp == 1) { // x^n
printf("-1 -1 -1 -1\n");
}
else {
ll g;
exgcd(f, d, g, c, e); // cf - de = a
if(a % g) {
printf("-1 -1 -1 -1\n");
continue;
}
c *= (a / g);
e *= (a / g);
if (c < 0 && e > 0)
printf("%lld %lld %lld %lld\n", e, f, -c, d);
else
printf("%lld %lld %lld %lld\n", c, d, -e, f);
}
}
}



G

代码

#include<bits/stdc++.h>
#define pb(a) push_back(a)
using namespace std;
const int maxn=8e5+5;
int fa[maxn];
vector<int> g[maxn];
int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void hb(vector<int>&x,vector<int>&y){
if(x.size()<y.size())swap(x,y);
for(int u:y)x.pb(u);
}

int main(){
int te;
cin>>te;
while(te--){
int n,m;
cin>>n>>m;
for(int i=0;i<n;++i)
fa[i]=i,g[i].clear();
int x,y;
for(int i=1;i<=m;++i){
cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
int q;
cin>>q;
while(q--){
cin>>x;
if(Find(x)!=x) continue;
vector<int>now=g[x];
g[x].clear();
for(auto v:now){
int f=Find(v);
if(f==x) continue;
hb(g[x],g[f]);
fa[f]=x;
}
}
for(int i=0;i<n;++i)
printf("%d ",Find(i));
puts("");

}
return 0;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

For the brave

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

07-12 291
08-08 79