Educational Codeforces Round 49

打edu的比赛确实要轻松多了,但是我真的是死脑筋,C题T了很久,其实不要遍历区间的写法反而短了很多并且一发就过了,但是 我当时一直不愿意去写,真的是。。。

A
判断回文

#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
#define ll long long
#define pb(i) push_back(i)
#define mp make_pair
using namespace std;
const int maxn=1e5+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef pair<int,int> PII;
ll fpow(ll n, ll k, ll p = mod) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n%p; n = n * n%p;} return r;}
ll inv(ll a, ll p = mod) {return fpow(a, p - 2, p);}
//head
string s;
bool check(int i,int j)
{
    char x,y;
    x=s[i]; y=s[j];
    if(x+1==y+1||x+1==y-1)return true;
    if(x-1==y+1||x-1==y-1)return true;
    else return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int flag=1;
        cin>>s;
        for(int i=0;i<n/2;i++)
        {
            if(!check(i,n-i-1)){flag=0;break;}
        }
        if(flag) puts("YES");
        else puts("NO");
    }
    return 0;
}

B
找规律

#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
#define ll long long
#define pb(i) push_back(i)
#define mp make_pair
using namespace std;
const int maxn=1e5+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef pair<int,int> PII;
ll fpow(ll n, ll k, ll p = mod) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n%p; n = n * n%p;} return r;}
ll inv(ll a, ll p = mod) {return fpow(a, p - 2, p);}
//head
string s;
ll n,q;
ll res;
ll solve(int x,int y)
{
    ll t = (x-1)/2;
    ll ans = (ll)t*n;
    if((x+y)%2) ans +=res;
    x = x - t*2;
    if(x==1) ans+= (ll)((y-1)/2)+1;
    else if(x==2)
    {
        if(n%2)
        {
            if(y%2) ans+=(n/2)+(y/2)+1; else ans+=(n/2)+1+y/2;
        }
        else ans+=(y-1)/2+1+n/2;
    }
   return ans;
}
int main()
{
    cin>>n>>q;
    res = (n*n+1)/2;
    for(int i=1;i<=q;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%I64d\n",solve(x,y));
    }
    return 0;
}

C 数学,求导

#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
#define ll long long
#define pb(i) push_back(i)
#define mp make_pair
using namespace std;
const int maxn=1e6+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef pair<int,int> PII;
ll fpow(ll n, ll k, ll p = mod) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n%p; n = n * n%p;} return r;}
ll inv(ll a, ll p = mod) {return fpow(a, p - 2, p);}
int a[maxn];
const int maxm=1e4+500;
int cnt[maxn];
int idx=0;
int v[maxm];
int read() {
    int x = 0;
    char c = getchar();
    while (c < '0' || c > '9')c = getchar();
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
int main()
{

    int t;
    scanf("%d",&t);
    while(t--)
    {
        idx=0;
        int n;
        scanf("%d",&n);
        int res=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n);
        int x,y;
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            int k=a[i];
            int p=upper_bound(a+1,a+1+n,k)-lower_bound(a+1,a+1+n,k);
            if(p>=4)
            {
                flag=1;
                printf("%d %d %d %d\n",k,k,k,k);break;
            }
            else if(p>=2)
            {
               v[idx++]=a[i];
            }
            i+=p-1;
        }
        if(flag)continue;
        sort(v,v+idx);
        int len = idx;
        int p0=v[0],q0=v[1];
        for(int i=0;i<len-1;i++)
        {
            //cout<<v[i]<<" ";
            int p =v[i],q=v[i+1];
            if(p*q0>q*p0)
            {
                p0=p;
                q0=q;
            }
        }
        printf("%d %d %d %d\n",p0,p0,q0,q0);
    }
    return 0;
}

D 拓扑排序找环

#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
#define ll long long
#define pb(i) push_back(i)
#define mp make_pair
using namespace std;
const int maxn=2e5+50;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef pair<int,int> PII;
ll fpow(ll n, ll k, ll p = mod) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n%p; n = n * n%p;} return r;}
ll inv(ll a, ll p = mod) {return fpow(a, p - 2, p);}
int in[maxn];
int vis[maxn];
ll a[maxn];
int g[maxn];
int n;
ll ans=0;
vector<int>c;
void check(int y)
{
    if(vis[y])return;
    ll res =inf;
    c.push_back(y);
    vis[y]=1;
    int tmp = g[y];
    res =min(a[tmp],a[y]);
    while(!vis[tmp])
    {
        vis[tmp]=1;
        tmp = g[tmp];

        res=min(res,a[tmp]);
    }
    ans+=res;
}
void solve()
{
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
        if(!in[i])
        {
            q.push(i);
        }
    }
    while(!q.empty())
    {
        int tmp=q.front();
        q.pop();
        int son =g[tmp];
        in[son]--;
        if(in[son]==0) q.push(son);
    }
}
int main()
{

    cin>>n;cl(vis);cl(in);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        g[i]=x;
        in[x]++;
    }
    solve();
    for(int i=1;i<=n;i++)
    {
        if(in[i])check(i);
    }
    cout<<ans<<endl;
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-29 12:19
点赞 评论 收藏
分享
10-24 13:36
门头沟学院 Java
Zzzzoooo:更新:今天下午有hr联系我去不去客户端,拒了
点赞 评论 收藏
分享
11-15 18:39
已编辑
西安交通大学 Java
全村最靓的仔仔:卧槽,佬啥bg呢,本也是西交么
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务