Summer training round2 #9(Training28)

A:签到题

C:模拟搜索题

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1010;
int year[505];
int where[505];
int visit[505];
vector<int> manager[505];
int anser=999999;
void dfs(int x)
{
visit[x]=1;
int len=manager[x].size();
for(int i=0;i<len;i++)
{
int to=manager[x][i];
if(!visit[to])
{
anser=min(anser,year[to]);
dfs(to);
}
}
}
int main()
{
        for(int i=1;i<=505;i++)
        where[i]=i;
        int n,m,I;
        int from,to;
        cin >> n >>m >>I;
        for(int i=1;i<=n;i++)
        cin >>year[i];
        for(int i=1;i<=m;i++)
        {
        scanf("%d %d",&from,&to);
        manager[to].pb(from);
        }
        string a;
        int cnt;
        for(int i=1;i<=I;i++)
        {
        cin >> a;
        if(a[0]=='T')
        {
        cin >> from >>to;
        swap(where[from],where[to]);
        swap(year[where[from]],year[where[to]]);
        }
        else if(a[0]=='P')
        {
        mem(visit,0);
        cin >> from;
        anser=999999;
        dfs(where[from]);
        if(anser==999999)
        cout<<"*"<<endl;
        else
        cout<<anser<<endl;
        }
        }
}
View Code

D:队友

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define INF 2139062143
#define inf -2139062144
#define ll long long
#include<vector>
using namespace std;
map<vector<int> , int> ma;
vector<int> rr;
bool jj = false;
vector<int> fold(int p,vector<int> qq) {
    int l = qq.size();
    vector<int> re;
    for(int i=1; i<=l; i++) {
        int tt = 0;
        bool flag = false;
        if(p - i >= 0) {
            tt+=qq[p-i];
            flag = true;
        }
        if(p + i - 1 < l) {
            tt+=qq[p + i - 1];
            flag = true;
        }
        if(flag)    re.insert(re.begin(),tt);
    }
    return re;
}
void dfs(vector<int> qq) {
    int llen = qq.size();
    int i,j;
    for(i=0; i<=llen && jj == false; i++) {
        vector<int> tem = fold(i,qq);
        if(tem == rr) {
            jj = true;
            break;
        }
        if(ma[tem] == 0) {
            ma[tem] = 1;
            dfs(tem);
        }
    }
}
int main() {
    vector<int> q;
    int n,i,j;
    scanf("%d",&n);
    for(i=0; i<n; i++) {
        int a;
        scanf("%d",&a);
        q.push_back(a);
    }
    int m;
    scanf("%d",&m);
    for(i=0; i<m; i++) {
        int a;
        scanf("%d",&a);
        rr.push_back(a);
    }
    dfs(q);
//    for(i=0; i<=n; i++) {
//        vector<int> tem = fold(i);
//        for(j=0; j<tem.size(); j++) {
//            printf("%d ",tem[j]);
//        }
//        printf("\n");
//        if(tem == rr) {
//            jj = true;
//            break;
//        }
//    }
    if(jj)    printf("S\n");
    else    printf("N\n");
    return 0;
}
View Code

E:水题

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1005;
int a[105];
int visit[1005];
queue<int> que;
int main()
{
      mem(visit,0);
      int r,n;
      cin >>r >> n;
      int now;
      for(int i=1;i<=n;i++)
      {
        scanf("%d",&now);
        visit[now]=1;
      }
      for(int i=1;i<=r;i++)
        if(!visit[i])
        que.push(i);
      int len=que.size();
      if(len==0)
      {
        cout<<"*"<<endl;
        return 0;
      }
      while(!que.empty())
      {
        cout<<que.front()<<" ";
        que.pop();
      }
      cout<<endl;
}
View Code

F:队友

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define INF 2139062143
#define inf -2139062144
#define ll long long
using namespace std;
int a[100005];
int p[100005];
map<int,int> q;
int main(){
    int n,i,j;
    scanf("%d",&n);
    int sum = 0;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum += a[i];
        p[i] = sum;
        q[sum] = 1;
//        printf("%d ",sum);
    }
//    printf("\n");
    a[0] = 0;
    q[0] = 1;
    p[0] = 0;
    if(sum % 3 != 0){
        printf("0\n");
        return 0;
    }
    int len = sum / 3;
    int ans = 0;
    for(i = 0;i < n;i++){
//        printf("%d %d %d\n",p[i],(p[i] + len) % sum,(p[i] + len * 2) % sum);
        if(q[(p[i] + len) % sum] == 1 && q[(p[i] + len * 2) % sum] == 1){
            ans++;
        }
    }
    printf("%d\n",ans/3);
    return 0;
}
View Code

G:队友

#include <bits/stdc++.h>
using namespace std;
struct Node {
    int val, id;
    bool operator < (const Node & rhs) const {
        return val < rhs.val;
    }
}a[305];
int L, C, X[305][305], cnt_l[30000005], cnt_c[30000005], l[305], c[305], b[305];
int Solve(int * array, int n) {
    for(int i = 1; i <= n; i++) a[i].val = array[i], a[i].id = i;
    sort(a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++) b[i] = a[i].id;
    int ans = 0;
    for(int i = 1; i <= n ;i++) {
        if(b[i] != i) {
            for(int j = i; j <= n; j++) {
                if(b[j] == i) {
                    swap(b[j], b[i]);
                    ans++;
                    break;
                }
            }
        }
    }
    return ans;
}
int main() {
    scanf("%d%d", &L, &C);
    for(int i = 1; i <= L; i++) {
        for(int j = 1; j <= C; j++) {
            scanf("%d", &X[i][j]);
        }
    }
    int flag1 = 0, flag2 = 0, cnt, sum;
    for(int i = 1; i <= L; i++) {
        cnt_l[(i - 1) * C * C + (1 + C) * C / 2] = 1;
        //cout << (i - 1) * C * C + (1 + C) * C / 2 << endl;
    }
    for(int i = 1; i <= C; i++) {
        cnt_c[i * L + C * (L - 1) * L / 2] = 1;
        //cout << i * L + C * (L - 1) * L / 2 << endl;
    }
    cnt = 0;
    for(int i = 1; i <= L; i++) {
        sum = 0;
        for(int j = 1; j <= C; j++) {
            sum += X[i][j];
        }
        //cout << sum << endl;
        if(cnt_l[sum]) cnt++;
    }
    //cout << cnt << endl;
    if(cnt == L) flag1 = 1;
    cnt = 0;
    for(int j = 1; j <= C; j++) {
        sum = 0;
        for(int i = 1; i <= L; i++) {
            sum += X[i][j];
        }
        if(cnt_c[sum]) cnt++;
    }
    if(cnt == C) flag2 = 1;
    //cout << flag1 << " " << flag2 << endl;
    if(flag1 && flag2) {
        for(int i = 1; i <= L; i++) {
            l[i] = X[i][1];
        }
        for(int i = 1; i <= C; i++) {
            c[i] = X[1][i];
        }
        int ans = 0;
        ans += Solve(l, L);
        ans += Solve(c, C);
        printf("%d\n", ans);
    }else {
        printf("*\n");
    }
    return 0;
}
View Code

H:数论题 矩阵快速幂

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define INF 2139062143
#define inf -2139062144
#define ll long long
using namespace std;
ll mod = 1000000;
struct matrix {
    ll sizex,sizey;
    ll num[100][100];
};
matrix mul(matrix a,matrix b) {
    matrix result;
    result.sizex = a.sizex;
    result.sizey = b.sizey;
    ll i,j,k;
    for(i=0; i<a.sizex; i++) {
        for(j=0; j<b.sizey; j++) {
            ll sum = 0;
            for(k=0; k<a.sizey; k++)    sum+=a.num[i][k]*b.num[k][j] % mod;
            result.num[i][j] = sum % mod;
        }
    }
    return result;
}

matrix zero(ll size) {
    ll i,j;
    matrix result;
    result.sizex = size;
    result.sizey = size;
    for(i=0; i<size; i++)
        for(j=0; j<size; j++)
            result.num[i][j] = 0;
    return result;
}

matrix eye(ll size) {
    matrix result;
    result.sizex = size;
    result.sizey = size;
    result = zero(size);
    for(ll i=0; i<size; i++) {
        result.num[i][i] = 1;
    }
    return result;
}

matrix quickmat(matrix a,ll k) {
    matrix origin = a;
    matrix res;
    res = eye(a.sizex);
    while(k) {
        if (k & 1) {
            res=mul(res,origin);
        }
        k>>=1;
        origin=mul(origin,origin);
    }
    return res;
}

matrix initial(ll *a,ll sizex,ll sizey) {
    ll i,j;
    matrix result;
    result.sizex = sizex;
    result.sizey = sizey;
    ll count = 0;
    for(i=0; i<sizex; i++)
        for(j=0; j<sizey; j++)
            result.num[i][j] = a[count++];
    return result;
}

ll all_ele_sum(matrix a) {
    ll re = 0;
    for(ll i = 0; i<a.sizex; i++) {
        for(ll j =0 ; j<a.sizey; j++) {
            re+=a.num[i][j];
        }
    }
    return re;
}

void printmatrix(matrix a) {
    for(ll i = 0; i<a.sizex; i++) {
        for(ll j =0 ; j<a.sizey; j++) {
            printf("%lld    ",a.num[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
int main() {
    ll n,k,l;
    scanf("%lld%lld%lld",&n,&k,&l);
    n /= 5;
    k %= mod;
    l %= mod;
    ll a[4] = {k,l,1,0};
    if(n == 1){
        printf("%06lld\n",a[0]);
        return 0;
    }
    ll x2 = (k * k % mod + l) % mod;
    if(n == 2){
        printf("%06lld\n",x2);
        return 0;
    }
    matrix temp = initial(a,2,2);
    matrix re = quickmat(temp,n-2);
    ll ans = (re.num[0][0] * x2 % mod + re.num[0][1] * k % mod) % mod;
    printf("%06lld\n",ans);
    return 0;
}
View Code

I:类搜 DP

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1010;
int f[maxn];
int n;
int c;
int t1,t2;
int tmax,tmin;
ll ans[maxn];
ll anser;
ll get(int x)
{
 if(x>n)
 return 0;
 if(ans[x]!=-1)
 return ans[x];
 ans[x]=2<<20;
 for(int i=x;i<=n;i++)
 {
        ll cha=f[i]-f[x];
        if(cha>tmax)
        break;
        if(cha<=t1)
        ans[x]=min(ans[x],get(i+1)+t1);
        if(cha<=t2)
        ans[x]=min(ans[x],get(i+1)+t2);
 }
 return ans[x];
}
int main()
{
        mem(ans,-1);
        cin >> n >> c >> t1 >> t2;
        tmax=max(t1,t2);
        tmin=min(t1,t2);
        for(int i=1;i<=n;i++)
        cin >> f[i];
        sort(f+1,f+1+n);
        ll anser=get(1);
        cout<<anser<<endl;
}
View Code

J:LCA/LCT

#include <bits/stdc++.h>
#define MAXN 20005
#define MAXM 100005
#define inf 0x3f3f3f3f
using namespace std;

int n, m, s;
int head[MAXN], cnt;
int fa[MAXN];
int ancient[MAXN][20], minw[MAXN][20], dep[MAXN];

struct Node {
    int u, v, w;
    bool operator<(const Node &rhs) const {
        return w > rhs.w;
    }
} ori_edge[MAXM];

struct Edge {
    int to, nxt, w;
} edge[MAXM * 2];

int Find(int x) {
    return fa[x] == x ? x : fa[x] = Find(fa[x]);
}

void AddEdge(int u, int v, int w) {
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].nxt = head[u];
    head[u] = cnt++;
}

void Add(int u, int v, int w) {
    AddEdge(u, v, w);
    AddEdge(v, u, w);
}

void Init() {
    memset(head, -1, sizeof(head));
    cnt = 0;
    for (int i = 1; i <= n; i++) fa[i] = i;
}

void Kruskal() {
    sort(ori_edge + 1, ori_edge + m + 1);
    int cnt = 0;
    for (int i = 1; i <= m; i++) {
        int f1 = Find(ori_edge[i].u);
        int f2 = Find(ori_edge[i].v);
        if (f1 != f2) {
            fa[f1] = f2;
            Add(ori_edge[i].u, ori_edge[i].v, ori_edge[i].w);
            cnt++;
            if (cnt == n - 1) break;
        }
    }
}

void Dfs(int u, int f, int d) {
    fa[u] = f;
    dep[u] = d;
    for (int i = head[u]; ~i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (v == f) continue;
        minw[v][0] = edge[i].w;
        Dfs(v, u, d + 1);
    }
}

void Pre() {
    memset(ancient, -1, sizeof(ancient));
    for (int i = 1; i <= n; i++) {
        ancient[i][0] = fa[i];
    }
    minw[1][0] = inf;
    for (int j = 1; (1 << j) <= n; j++) {
        for (int i = 1; i <= n; i++) {
            if (~ancient[i][j - 1]) {
                int f = ancient[i][j - 1];
                ancient[i][j] = ancient[f][j - 1];
                minw[i][j] = min(minw[i][j - 1], minw[f][j - 1]);
            }
        }
    }
}

int Query(int p, int q) {
    if (dep[p] < dep[q]) swap(p, q);
    int k = 0;
    while ((1 << k) < dep[p]) k++;
    int ans = inf;
    for (int i = k; i >= 0; i--) {
        if (~ancient[p][i] && dep[ancient[p][i]] >= dep[q]) {
            ans = min(ans, minw[p][i]), p = ancient[p][i];
        }

    }

    if (p == q) return ans;
    for (int i = k; i >= 0; i--) {
        if (~ancient[p][i] && ancient[p][i] != ancient[q][i]) {
            ans = min(ans, minw[p][i]);
            p = ancient[p][i];
            ans = min(ans, minw[q][i]);
            q = ancient[q][i];
        }
    }

    if (p != q) {
        ans = min(ans, minw[p][0]);
        ans = min(ans, minw[q][0]);
    }
    return ans;
}

int main() {
    scanf("%d%d%d", &n, &m, &s);
    Init();
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d", &ori_edge[i].u, &ori_edge[i].v, &ori_edge[i].w);
    }
    Kruskal();
    Dfs(1, -1, 1);
    Pre();
    for (int i = 1; i <= s; i++) {
        int l, h;
        scanf("%d%d", &l, &h);
        printf("%d\n", Query(l, h));
    }
    return 0;
}
View Code

 

posted @ 2017-08-15 23:12  Aragaki  阅读(181)  评论(0编辑  收藏  举报