复制代码

Codeforces Round 493

心情不好,被遣散回学校 ,心态不好 ,为什么会累,一直微笑就好了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    freopen("in","r",stdin);\
         freopen("out","w",stdout);

    int n ,k=0,s=0,a[10];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]<a[k]) k=i;
        s+=a[i];
    }
    if(n<2||a[k]==s-a[k]){
        cout<<-1;
    }else{
        cout<<1<<" "<<k+1<<endl;
    } 
}
View Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)1e4+5;
ll arr[maxn],n,k;
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>arr[i];
    ll num1=0 ,num2=0;
    vector<long long> v;
    for(int i=0;i<n-1;i++)
    {
        if(arr[i]&1)num1++;
        else num2++;
        if(num1    == num2) 
            v.push_back(abs(arr[i]-arr[i+1]));
    }
     ll s= 0,ans=0;
    if(v.size()==0) cout<<0<<endl;
    else{
        sort(v.begin(),v.end());
        for(int i=0;i<v.size();i++)
        {
            s+=v[i];
            if(s>k)break;
            ans++;
        }
        cout<<ans<<endl;
    }


}
View Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    freopen("in","r",stdin);\
         freopen("out","w",stdout); 

    string str;
    ll n,m,k;
    cin>>n>>m>>k>>str;
    vector<int> v;
    int ans=0;
    if(str[0]=='0')ans =1;
    for(int i=1;i<n;i++)
    {
         if(str[i]=='0'&&str[i-1]!='0') ans++;
    }
    ll cnt = LLONG_MAX;
    for(int i=0;i<ans;i++)
    {
        cnt = min(cnt,m*i+k*(ans-i));
    }
    cout<<(cnt==LLONG_MAX?0:cnt) <<endl;
}
View Code
#include<bits/stdc++.h>
using namespace std;
#define REP(i,l,r) for(int i=l;i<=r;i++)
int main()
{
    set<long long> s;
    long long n;
    cin>>n;
    if(n<=10)
    {
        REP(i,0,n) REP(j,0,n)REP(k,0,n)REP(l,0,n)
        {
            if(i+j+k+l==n) s.insert(50*l+10*k+5*j+i);
        }
        cout<<s.size()<<endl;
    }
    else cout<<49ll*n-247<<endl;
}
View Code
#include<iostream>

using namespace std;

typedef long long ll;

const ll mod = 998244353;

ll pow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1) {
            ans = ans * a % mod;
        }
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}

int main() {
    ll n ;
    cin>>n;
    ll ans =0,cur= 1;
    for(int i=1;i<=n;i++)
    {
        cur = cur*(n+1-i)*pow(i,mod-2);
        ll sign = pow(-1,i+1);
        ll val = pow(3,i)*pow(3,n*(n-i));
        ans = ans+(cur*sign*val);
    }
    ans = ans*2;

    cur =1 ;
    #if 0
     {
        cur = cur * (N + 1 - i) % mod * pow(i, mod - 2) % mod;
        ll sign = pow(-1, i + 1);
        ll val = pow(3, i) * pow(3, N * (N - i)) % mod;
        ans = (ans + (cur * sign * val) % mod) % mod;
    }
    ans = ans * 2 % mod;

    cur = 1;
    for (int 
        i = 0; i < N; ++i) {
        ll sign = pow(-1, i + 1);
        ll val = (pow(-pow(3, i) + 1, N) - (pow(-pow(3, i), N))) % mod * 3 % mod;
        ans = (ans + cur * sign * val % mod) % mod;
        cur = cur * (N - i) % mod * pow(i + 1, mod - 2) % mod;
    }

    ans = (ans + mod) % mod;
    cout << ans << endl;
    return 0;
    #endif 
}
View Code
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353 ;
ll ans,n;
ll q_pow(ll x,ll y)
{
    ll ans =1;
    while(y)
    {
        if(y&1) ans = ans*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return ans;
}
int main()
{
    cin>>n;
    ans  = q_pow(3,n*n) - q_pow(q_pow(3,n)-3,n);
    for(ll i =0 ,t=1;i<=n;i++)
    {

        ll b = q_pow(3,n-i);
        ll res = t*(q_pow(b,n) - q_pow(b-(i?1:3)+mod,n))%mod;
        if(i) res = res*3%mod;
        ans+=i&1?-res:res;
        t =t*(n-i)%mod*q_pow(i+1,mod-2)%mod;
    }
    cout<<(ans%mod+mod)%mod;
}
View Code
#include<bits/stdc++.h>
using namespace std; 
using namespace std;

typedef long long ll;

const int N = 4010, K = 80, P = 998244353;

int k;
int comb[K][K];
int a1[K], a2[K];
pair<int, int> t1[N], t2[N];

void solve(int n, pair<int, int>* t, int* a);

int main() {
    int n, m;
    scanf("%d%d%d", &n, &m, &k);
    if (k % 2) {
        puts("0");
        return 0;
    }
    k /= 2;
    for (int i = 1; i < n; ++i)
        scanf("%d%d", &t1[i].first, &t1[i].second);
    for (int i = 1; i < m; ++i)
        scanf("%d%d", &t2[i].first, &t2[i].second);
    solve(n, t1, a1);
    solve(m, t2, a2);
    for (int i = 0; i <= k * 2; ++i) {
        comb[i][0] = 1;
        for (int j = 1; j <= i; ++j) {
            comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
            if (comb[i][j] >= P)
                comb[i][j] -= P;
        }
    }
    int ans = 0;
    for (int i = 0; i <= k; ++i)
        ans = (ans + (ll)comb[k * 2][i * 2] * a1[i] % P * a2[k - i] % P) % P;
    printf("%d\n", ans);
    return 0;
}

void solve(int n, pair<int, int>* t, int* a) {
    static int dp[K][N * 3], sum[K][N];
    memset(dp, 0, sizeof(dp));
    memset(sum, 0, sizeof(sum));
    for (int i = 0; i <= n * 3 - 2; ++i)
        dp[0][i] = 1;
    for (int i = 1; i <= k; ++i) {
        for (int e = 1; e < n; ++e) {
            int u = t[e].first, v = t[e].second;
            sum[i - 1][u] += dp[i - 1][n * 2 - 1 + e];
            if (sum[i - 1][u] >= P)
                sum[i - 1][u] -= P;
            sum[i - 1][v] += dp[i - 1][n + e];
            if (sum[i - 1][v] >= P)
                sum[i - 1][v] -= P;
        }
        for (int j = 0; j < i; ++j) {
            for (int e = 1; e < n; ++e) {
                int u = t[e].first, v = t[e].second;
                dp[i][u] = (dp[i][u] + dp[j][u] * (ll)dp[i - j - 1][n * 2 - 1 + e]) % P;
                dp[i][v] = (dp[i][v] + dp[j][v] * (ll)dp[i - j - 1][n + e]) % P;
                dp[i][n + e] = (dp[i][n + e] + dp[j][n + e] * (ll)(sum[i - j - 1][u] - dp[i - j - 1][n * 2 - 1 + e]) % P + P) % P;
                dp[i][n * 2 - 1 + e] = (dp[i][n * 2 - 1 + e] + dp[j][n * 2 - 1 + e] * (ll)(sum[i - j - 1][v] - dp[i - j - 1][n + e]) % P + P) % P;
            }
        }
    }
    for (int i = 0; i <= k; ++i) {
        for (int u = 1; u <= n; ++u) {
            a[i] += dp[i][u];
            if (a[i] >= P)
                a[i] -= P;
        }
    }
}
View Code
这个真的没有心情切掉,看看诡异的算法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x)*(x))
#define mp make_pair
#define uint unsigned
#define PI pair<int,int>
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
    ll x = 0; char ch = gc(); bool positive = 1;
    for (; !isdigit(ch); ch = gc())    if (ch == '-')    positive = 0;
    for (; isdigit(ch); ch = gc())    x = x * 10 + ch - '0';
    return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
        a=-a; putchar('-');
    }
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
    return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=120005,block=300,B=N/block+3,inf=1000000005;
int n,q,a[N],belong[N],mzs[N],p[N],l[N],r[N],q1[N],q2[N],t1,t2,val[N],cnt[B],lazy[B],L[B],R[B];
PI mn[N];
ll ans[N];
PI operator +(PI a,PI b){
    if(a.second==0)return b; else if(b.second==0)return a;
    return a.first==b.first?mp(a.first,a.second+b.second):(a.first>b.first?a:b);
}
void bao(int l,int r,int x){
    //cout<<l<<" "<<r<<" "<<x<<endl;
    int id=belong[l];
    for(int i=l;i<=r;i++)a[i]+=x;
    mn[id]=mp(inf,0); 
    for(int i=L[id];i<=R[id];i++)mn[id]=mn[id]+mp(a[i],1);
}
void down(int x){
    if(!mzs[x])return;
    for(int i=L[x];i<=R[x];i++)if(a[i]==mn[x].first){
        val[i]+=mzs[x];
    }
    mzs[x]=0;
}
void ins(int l,int r,int x){
    if(l>r)return;
    if(belong[l]==belong[r]){
        down(belong[l]); bao(l,r,x); return;
    }
    down(belong[l]); bao(l,R[belong[l]],x);
    down(belong[r]); bao(L[belong[r]],r,x);
    for(int i=belong[l]+1;i<belong[r];i++)lazy[i]+=x;
}
void upd(){
    for(int i=1;i<=belong[n];i++)if(mn[i].first+lazy[i]==0){
        mzs[i]++; cnt[i]+=mn[i].second;
    }
}
ll ask(int l){
    ll ans=0; down(belong[l]);
    for(int i=l;i<=R[belong[l]];i++)ans+=val[i];
    for(int i=belong[l]+1;i<=belong[n];i++)ans+=cnt[i];
    return ans;
}
vector<int> v[N];
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        p[i]=read();
        if((i-1)%block==0)
            L[belong[i]=belong[i-1]+1]=i; 
        else belong[i]=belong[i-1];
        R[belong[i]]=i;
         a[i]=-1; 
         mn[belong[i]]=mn[belong[i]]+mp(-1,1);
    }
    q=read();
    for(int i=1;i<=q;i++){
        l[i]=read(); r[i]=read();
        v[r[i]].push_back(i);
    }
    for(int i=1;i<=n;i++){
        while(p[q1[t1]]<p[i]&&t1){
            ins(q1[t1-1]+1,q1[t1],-p[i]+p[q1[t1]]); t1--; 
        }
        q1[++t1]=i;
        while(p[q2[t2]]>p[i]&&t2){
            ins(q2[t2-1]+1,q2[t2],p[i]-p[q2[t2]]); t2--;
        }
        q2[++t2]=i;
        ins(1,i,1); 
        upd();
        //for(int i=1;i<=n;i++)cout<<a[i]<<" "; puts("");
        //cout<<mzs[1]<<" "<<mn[1].first<<" szb"<<lazy[1]<<" "<<cnt[2]<<" "<<endl;
        for(unsigned j=0;j<v[i].size();j++)ans[v[i][j]]=ask(l[v[i][j]]);
    }
    for(int i=1;i<=q;i++)writeln(ans[i]);
} 
View Code

 

posted @ 2018-07-10 14:06  pg633  阅读(133)  评论(0编辑  收藏  举报