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;
        }
        }
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

时间: 2024-12-11 16:35:07

Summer training round2 #9(Training28)的相关文章

Summer training round2 #8(Training26)

A:贪心DFS 先从最远的搜起 如果一个点的value>=2 就ans++ D:并查集 E:大模拟 F:快速幂 #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL > pLL; const LL MOD1 = 1e9 + 7, MOD2 = 1e9 + 9; int n, a[1000], vis[1000], i, j, k; set<pLL&g

Summer training round2 #6 (Training #26)

A:树遍历 DFS 贪心 先从最远的点开始判 如果某个点连有的算上自身的junction不小于二就ans++ return 0 连有的算上自身的junction为一 就return 1 #include <bits/stdc++.h> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define MOD 1000000007 #define

Summer training round2 #5

A:正着DFS一次处理出每个节点有多少个优先级比他低的(包括自己)作为值v[i] 求A B 再反着DFS求优先级比自己高的求C #include <bits/stdc++.h> #include <cstring> #include <iostream> #include <algorithm> #define EPS 1.0e-9 #define PI acos(-1.0) #define INF 30000000 #define MOD 10000000

Summer training round2 #1

A:水 B:求两个三角形之间的位置关系:相交 相离 内含 ①用三个点是否在三角形内外判断    计算MA*MB.MB*MC.MC*MA的大小 若这三个值同号,那么在三角形的内部,异号在外部 #include<stdio.h> #include<math.h> #include<string.h> #include<iostream> #include<algorithm> #define INF 2139062143 #define inf -2

Android官方开发文档Training系列课程中文版:手势处理之滚动动画及Scroller

原文地址:http://android.xsoftlab.net/training/gestures/scroll.html 在Android中,滑动经常由ScrollView类来实现.任何超出容器边界的布局都应该将自己内嵌在ScrollView中,以便提供可滚动的视图效果.自定义滚动只有在特定的场景下才会被用到.这节课将会描述这样一种场景:使用scroller显示一种可滚动的效果. 你可以使用Scroller或者OverScroller来收集一些滑动动画所需要的数据.这两个类很相似,但是Ove

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

WeChall_Training: Programming 1 (Training, Coding)

When you visit this link you receive a message.Submit the same message back to http://www.wechall.net/challenge/training/programming1/index.php?answer=the_messageYour timelimit is 1.337 seconds 解题: 先在浏览器获取自己的cookie,再用python写了个自动提交的程序,header加上自己的cooki

Training的第二十天

今天接着做前天的活,就是实现把下载下来的jpg格式的图片设置为桌面的功能.要实现这个功能的关键点一个是要调用API(SystemParametersInfo)更换桌面:二是要把下载下来的JPG格式的图片转换成Bmp格式的图片才能够应用为桌面. 由于对API没什么概念,所以我从网上查了下资料并从网上copy了有关该API变成C#的代码和把jpg格式的图片转换成bmp格式图片的代码.接着看懂了里面的代码便自己修改了一下.接下来的是完善这个程序和把它加入到windows自动启动的任务中. 对于程序的完

2014 Multi-University Training Contest 6 Apple Tree(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 120 Problem Description I've bought an orchard an