1. 程式人生 > >[多校補題]2017 Multi-University Training Contest 6 solutions BY 福州大學

[多校補題]2017 Multi-University Training Contest 6 solutions BY 福州大學

1003 Inversion

Inversion

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1826    Accepted Submission(s): 868


Problem Description Give an array A, the index starts from 1.
Now we want to know Bi=maxijAj , i2.

Input The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer n : the size of array A.
Next one line contains n integers, separated by space, ith number is A
i
.

Limits
T20
2n100000
1Ai1000000000
n700000
Output For each test case output one line contains n-1 integers, separated by space, ith number is Bi+1.
Sample Input 2 4 1 2 3 4 4 1 4 2 3
Sample Output 3 4 3 2 4 4

需要統計的區間最大值次數是O(nlog(n))級別的。時間複雜度O(nlog(n))。優秀的多個O(log(n))的做法也是可以卡過的。

這裡介紹一下優雅的暴力做法。將AA陣列按值從大到小排序,對於每個下標i

暴力找到最大的不被iii整除的數。時間複雜度O(nlog(n))

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;

struct pp{
    int number,val;
    bool operator <(pp const&b)const{
        return val<b.val;
    }
}a[100005];

vector<int>hello;

int main(){
    int t;
    while(~scanf("%d",&t)){
        while(t--){
            int n;
            hello.clear();
            scanf("%d",&n);
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i].val);
                a[i].number=i;
            } 
            sort(a+1,a+n+1);
            for(int i=2;i<=n;i++){
                int maxn=0;
                for(int j=n;j>=1;j--){
                    if(a[j].number%i!=0){
                        hello.push_back(a[j].val);
                        break;
                    }
                }    
            }
            for(int i=0;i<hello.size();i++){
                if(i>0)printf(" ");
                printf("%d",hello[i]);
            }
            printf("\n");
        }
    }
    return 0;    
}


1008 Kirinriki

Kirinriki

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3009    Accepted Submission(s): 441


Problem Description We define the distance of two strings A and B with same length n is
disA,B=i=0n1|AiBn1i|
The difference between the two characters is defined as the difference in ASCII.
You should find the maximum length of two non-overlapping substrings in given string S, and the distance between them are less then or equal to m.

Input The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integers m : the limit distance of substring.
Then a string S follow.

Limits
T100
0m5000
Each character in the string is lowercase letter, 2|S|5000
|S|20000

Output For each test case output one interge denotes the answer : the maximum length of the substring.

Sample Input 1 5 abcdefedcb
Sample Output 5 Hint[0, 4] abcde [5, 9] fedcb The distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5

標題提示我們迴文串。

兩個不重合的子串向中心一起延長會形成奇偶長度兩種合串。列舉一下中心向外延伸,如果和超過了閾值彈掉中心處的位置。雙指標維護。時間複雜度O(n2)

另解:列舉[1,i],[j,n]用同樣方法往內縮。時間複雜度O(n2)

老司機隊做法:發現所選元素肯定是在對角線上的元素,那麼我們就遍歷兩條對角線 來看一看滿足長度為m的最大長度是多少

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <set>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 8010;
char in[maxn];
//int dis[maxn][maxn];
queue<int> que;
int main(){
    //freopen("input.txt", "r", stdin);
    int t;
    int i,j,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&m);
        scanf("%s",in+1);
        int len = strlen(in+1);
        int save;
        int sum,res = -1;
        for(i=1;i<len;i++){
            int x = i, y = i;
            sum = 0;
            //from dis[i][i]
            while(!que.empty()){
                que.pop();
            }
            x--,y++;
            while(x>=1&&y<=len){
                save = abs(in[x]-in[y]);
                que.push(save);
                sum += save;
                while(sum>m){
                    sum -= que.front();
                    que.pop();
                }
                x--, y++;
                int killme = que.size();
                res = max(res,killme);
                //cout<<"push "<<save<<" "<<que.size()<<" "<<sum<<" "<<res<<endl;
            }
            
            //from dis[i][i+1]
            x = i, y = i+1;
            sum = 0;
            while(!que.empty()){
                que.pop();
            }
            while(x>=1&&y<=len){
                save = abs(in[x]-in[y]);
                que.push(save);
                sum += save;
                while(sum>m){
                    sum -= que.front();
                    que.pop();
                }
                x--, y++;
                int killme = que.size();
                res = max(res,killme);
                //cout<<"push "<<save<<" "<<que.size()<<" "<<sum<<" "<<res<<endl;
            }
        }
        printf("%d\n",res);
    }
    return 0;
}


1010 Gameia(待補)

  • 如果Bob能把這棵樹分成若干兩個一組的點對,那麼Bob取得勝利,否則Alice獲勝。
  • 如果原樹不存在兩兩匹配的方案,Alice從樹葉開始,每次都染樹葉父節點,Bob被迫只能不斷的染葉子,Bob退化成一般玩家,因為Bob做不做小動作都不會逆轉局勢,總會出現一個時間點Bob沒辦法跟上Alice的節奏而讓Alice染到一個周圍都已被染色的孤立點(因為原樹不存在兩兩匹配的方案)
  • 如果原樹存在兩兩匹配的方案,而且Bob的小動作次數也足以把原樹分成兩兩的點對,那麼Bob顯然獲勝。
  • 如果原樹存在兩兩匹配的方案,而Bob的小動作不足以把樹分成兩兩的點對,Alice一定獲勝,因為每次染某個葉子節點(該節點為其父節點的唯一子節點),Alice總能迫使Bob不斷的做小動作以保證剩下的樹不會出現奇數節點的樹,且每次小動作割出一個點對(包含Alice剛染的點),最後有兩種情況。
  • 出現某個結點有>=2個子節點為葉子節點。Alice染這個點,Bob跟不上Alice的節奏,出現孤點,Ailice取勝
  • 否則整個過程一定會持續到樹被染光或者Bob被Alice掏空導致做不了小動作進而被迫割出一塊size為奇數的子樹(這棵樹顯然沒辦法兩兩匹配)而敗北。
  • Bob被允許“任意時刻”做小動作看似很厲害其實很雞肋,把問題改成“Bob只能在遊戲開始之前做小動作”會得到同樣的結論。
  • “氪不改命,玄不救非”

時間複雜度 O(n)O(n)O(n)


1011 Classes

Classes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2048    Accepted Submission(s): 895


Problem Description The school set up three elective courses, assuming that these courses are A, B, C. N classes of students enrolled in these courses.
Now the school wants to count the number of students who enrolled in at least one course in each class and records the maximum number of students.
Each class uploaded 7 data, the number of students enrolled in course A in the class, the number of students enrolled in course B, the number of students enrolled in course C, the number of students enrolled in course AB, the number of students enrolled in course BC, the number of students enrolled in course AC, the number of students enrolled in course ABC. The school can calculate the number of students in this class based on these 7 data.
However, due to statistical errors, some data are wrong and these data should be ignored.
Smart you must know how to write a program to find the maximum number of students.

Input The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer N, indicates the number of class.
Then N lines follow, each line contains 7 data: a, b, c, d, e, f, g, indicates the number of students enrolled in A, B, C, AB, BC, AC, ABC in this class. 
It's guaranteed that at least one data is right in each test case.

Limits
T100
1N100
0a,b,c,d,e,f,g100
Output For each test case output one line contains one integer denotes the max number of students who enrolled in at least one course among N classes.

Sample Input 2 2 4 5 4 4 3 2 2 5 3 1 2 0 0 0 2 0 4 10 2 3 4 9 6 12 6 3 5 3 2
Sample Output 7 15 Hint In the second test case, the data uploaded by Class 1 is wrong. Because we can't find a solution which satisfies the limitation. As for Class 2, we can calculate the number of students who only enrolled in course A is 2, the number of students who only enrolled in course B is 6, and nobody enrolled in course C, the number of students who only enrolled in courses A and B is 1, the number of students who only enrolled in courses B and C is 3, the number of students who only enrolled in courses A and C is 1, the number of students who enrolled in all courses is 2, so the total number in Class 2 is 2 + 6 + 0 + 1 + 3 + 1 + 2 = 15.
簽到題。

唯一的trick是要判一下Venn圖上每個部分不小於0。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int classes[110][10];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(classes,0,sizeof(classes));
        int n;
        scanf("%d",&n);
        for(int i = 1; i <= n ;i ++)
        {
            for(int j = 1; j <= 7 ;j ++)
            {
                scanf("%d",&classes[i][j]);
            }
        }
        
        int maxNum = 0;
        for(int i = 1; i <= n ;i ++)
        {
            if(classes[i][1] >= classes[i][4] && classes[i][1] >= classes[i][6])
            {
                if(classes[i][2] >= classes[i][4] && classes[i][2] >= classes[i][5])
                {
                    if(classes[i][3] >= classes[i][5] && classes[i][3] >= classes[i][6])
                    {
                        if(classes[i][5] >= classes[i][7] && classes[i][4] >= classes[i][7] && classes[i][6] >= classes[i][7])
                        {
                            if(classes[i][2] - classes[i][4] - classes[i][5] + classes[i][7] >=0)
                            {
                                if(classes[i][1] - classes[i][4] - classes[i][6] + classes[i][7] >=0)
                                {
                                    if(classes[i][3] - classes[i][5] - classes[i][6] + classes[i][7] >=0)
                                    {
                                        int temp = 0;
                                        for(int j = 1; j <= 3; j++)
                                            temp += classes[i][j];
                                            
                                        for(int j = 4; j <= 6; j++)
                                            temp -= classes[i][j];                            
                                
                                        temp += classes[i][7];    
                                            
                                        maxNum = max(maxNum,temp);    
                                    }
                                }
                            }
                        
                
                        }
                        
                    }
                }
            }
        }
        
        printf("%d\n",maxNum);
    }
    
    return 0;
 } 


相關推薦

[]2017 Multi-University Training Contest 6 solutions BY 福州大學

1003 Inversion Inversion Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1826 

[]2017 Multi-University Training Contest 7 solutions BY 杭州二中

1005 Euler theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1322    Accepted Submissio

2017 Multi-University Training Contest 5 solutions BY 吉如一

出題時難度安排大概是: Easy: 06,08,1106,08,11。 Medium-Easy:01,0901,09。 Medium:02,04,0702,04,07。 Medium-Hard:03,05,1003,05,10。 感覺全放中檔題無法阻止多人多機 AK 的鐵

ACM聯賽7 2018 Multi-University Training Contest 7 1009 Tree

ive tdi ini 我們 gis which print tmp bool 【題意概述】   給一棵以1為根的樹,樹上的每個節點有一個ai值,代表它可以傳送到自己的ai倍祖先,如果不存在則傳送出這棵樹。現在詢問某個節點傳送出這棵樹需要多少步。 【題解】   其實是把“

杭電2018第六場(2018 Multi-University Training Contest 6) 1001.oval-and-rectangle (HDU6362)-數學期望、微積分

style 草稿 隊友 training sin con queue turn .com 2018 Multi-University Training Contest 6 6362.oval-and-rectangle 題意就是橢圓裏畫內接矩形,問你矩形周長的期

杭電2018第六場(2018 Multi-University Training Contest 6) 1012.Pinball(HDU 6373) -簡單的計算幾何+物理受力分析

info 簡單的 垂直 -- vector 分析 code space cti 6373.Pinball 物理受力分析題目。 畫的有點醜,通過受力分析,先求出θ角,為arctan(b/a),就是atan(b/a),然後將重力加速度分解為垂直斜面的和平行斜面的,垂直

2017 Multi-University Training Contest - Team 3

E - RXD and dividing RXD has a tree TT, with the size of nn. Each edge has a cost.  Define f(S)f(S) as the the cost of th

2017 Multi-University Training Contest

RXD has a tree TT, with the size of nn. Each edge has a cost.  Define f(S)f(S) as the the cost of the minimal Steiner Tree of the set SS

2017 Multi-University Training Contest 10 1002 Array Challenge HDU 6172(找規律 矩陣快速冪)

題意:There’s an array that is generated by following rule.h0=2,h1=3,h2=6,hn=4hn−1+17hn−2−12hn−3−16And

HDU 6052 2017 Multi-University Training Contest

題意:給出一個n*m(1<=n,m<=100)的矩陣,每個矩陣元素有一個顏色值ai(0<=ai<=10000),現在定義一個子矩陣的value為子矩陣中不同顏色的數量,求子矩陣value的期望。 題解:期望=所有子矩陣的value總和/子矩陣個數。首

HDU 6058 2017 Multi-University Training Contest

題意:給定一個n(<=5e5)的全排列,以及一個 K 。定義value(l , r , k)=區間[ l , r ]中第 k 大的元素,如果區間長度 len = r - l + 1 < k 則 value( l , r , k ) = 0 求所有子序列的valu

2018 Multi-University Training Contest 6

聯通 push_back .net ++ 質數 ipa 合數 acm can Werewolf 只考慮村民邊,然後每個村民邊求聯通塊,最後加狼邊,看看兩個端點在不在一個聯通塊裏就可以了。 比賽的時候有點坑,思路正確,寫歪了…… #include <bits/std

hdu6365 2018 Multi-University Training Contest 6 1004 Shoot Game

ont 區間 永遠 線段 scan for 分割 unique scanf http://acm.hdu.edu.cn/showproblem.php?pid=6365 細節處理 unique返回的是最後一位的後一位,因此從1開始的數組要減去(p+1) 結構體可以用unq

2018 Multi-University Training Contest 6 Solution

first all aps 圖片 utl name nba image c++ A - oval-and-rectangle 題意:給出一個橢圓的a 和 b,在$[0, b]中隨機選擇c$ 使得四個頂點在橢圓上構成一個矩形,求矩形周長期望 思路:求出每種矩形的周長,除以b(

2018 Multi-University Training Contest 6 A oval-and-rectangle【精度】

用定積分推匯出答案是 2.0*b+a*pi 但是答案要求忽略掉第七位,所以不可以直接用double(四捨五入)。 要麼轉換成長整型,要麼將double第七位的隱患解決掉(感覺可以二分,很囉嗦但很

2018 Multi-University Training Contest 6 I Werewolf【博弈】

n個人互相說話,有的人是民(實話實說),有的人是狼(胡言亂語)。請判斷有多少人的身份可以確定。 根據題意,仔細思考可以發現,如果A說B是狼,B說A是民。那麼B是鐵狼。 假設B是民,則A是民,則A話有矛盾,故B是狼。 推廣開來,如果一條鏈上都是民,突然出

2018 Multi-University Training Contest 6 L Pinball【受力分析】

將物體的重力加速度分解。 一部分用來做彈起運動,一部分用來做勻加速直線運動。 可以計算得出兩個方向上的運動週期,兩者之商為答案。 #include <iostream> #in

2015 Multi-University Training Contest 6 題意理解和自己看到題目的想法(更新ing)

HDU5353:Average 題意: 有n個人組成一個環,相鄰的兩個人能互相給糖果,對於相鄰的兩個人而言,只能進行一次操作,要麼x給y一個糖果,要麼y給x一個糖果,要麼不動,問能否經過一定的操作使得每個人的糖果數一樣,並輸出步驟。 HDU5354:Bipartit

杭電2018第四場(2018 Multi-University Training Contest 4) 1004.Problem D. Nothing is Impossible (HDU6335) -思維

假設 spa 。。 lan 多校 () class span tdi 6335.Problem D. Nothing is Impossible 題意:給你n道題目,m個人,每題有x個正確選項,y個錯誤選項,問你做對題數量最多的人做對了多少道題目。 如果一道題有

2018 Multi-University Training Contest 3 杭電第三場

整數 其余 for cube cst ace 長度 get 每次 躺了幾天 終於記得來填坑了 1001 Ascending Rating (hdoj 6319) 鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6319 單調隊列