CSU-ACM2016暑假集训比赛8

本文解析了六道算法挑战赛题目,涉及字符串处理、动态规划、数学优化等方面,提供了完整的代码实现及思路说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - A
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 3746
Description
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of “HDU CakeMan”, he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl’s fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls’ lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet’s cycle is 9 and its cyclic count is 2:

Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.
CC is satisfied with his ideas and ask you for help.
Input
The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by ‘a’ ~’z’ characters. The length of the string Len: ( 3 <= Len <= 100000 ).
Output
For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.
Sample Input
3
aaa
abca
abcde
Sample Output
0
2
5

//A:n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
using namespace std;
const int MAX=1000005;
char S[MAX],P[MAX];
int Next[MAX],T;
int main()
{
    cin.sync_with_stdio(false);
    cin>>T;
    while (T--)
    {
        cin>>(S+1);
        memset(Next,0,sizeof(Next));Next[0]=-1;
        int len=strlen(S+1);
        for (int i=1;i<=len;i++)
        {
            int p=Next[i-1];
            while (p>=0&&S[p+1]!=S[i]) p=Next[p];
            Next[i]=p+1;
        }
        int ans=len-Next[len];
        if (Next[len]==0)
            cout<<len<<endl;
        else
        {
            if (len%ans==0)
                cout<<0<<endl;
            else
                cout<<ans-len%ans<<endl;
        }
    }
    return 0;
}

B - B
Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

SGU 104
Description

PROBLEM

You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.

Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.

V A S E S

1

2

3

4

5

Bunches

1 (azaleas)

7

23

-5

-24

16

2 (begonias)

5

21

-4

10

23

3 (carnations)

-21

5

-4

-20

20

According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.

To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.

ASSUMPTIONS

1 ≤ F ≤ 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.
F ≤ V ≤ 100 where V is the number of vases.
-50 £ Aij £ 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.
Input

The first line contains two numbers: F, V.
The following F lines: Each of these lines contains V integers, so that Aij is given as the j’th number on the (i+1)’st line of the input file.
Output

The first line will contain the sum of aesthetic values for your arrangement.
The second line must present the arrangement as a list of F numbers, so that the k’th number on this line identifies the vase in which the bunch k is put.
Sample Input

3 5
7 23 -5 -24 16
5 21 -4 10 23
-21 5 -4 -20 20
Sample Output

53
2 4 5


//B: dp 还原路径

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
using namespace std;
const int MAX=105;
int M,N,F[MAX][MAX],A[MAX][MAX],Path[MAX][MAX];
vector<int> Ans;
void print(int m,int n)
{
    if (m==0) return;
    if (Path[m][n]==1)
        print(m,n-1);
    if (Path[m][n]==0)
    {
        print(m-1,n-1);
        Ans.push_back(n);
    }
}
int main()
{
    cin.sync_with_stdio(false);
    while (cin>>M>>N)
    {
        Ans.clear();
        for (int i=1;i<=M;i++)
            for (int j=1;j<=N;j++)
                cin>>A[i][j];
        for (int i=0;i<MAX;i++)
            for (int j=0;j<MAX;j++)
                F[i][j]=-99999999;
        for (int j=0;j<MAX;j++)
            F[0][j]=0;
        memset(Path,-1,sizeof(Path));
        for (int i=1;i<=M;i++)
        {
            for (int j=1;j<=N;j++)
            {
                int F1=F[i][j-1];
                int F2=F[i-1][j-1]+A[i][j];
                if (F1>F2)
                {
                    F[i][j]=F1;
                    Path[i][j]=1;
                }
                else
                {
                    F[i][j]=F2;
                    Path[i][j]=0;
                }
            }
        }
        cout<<F[M][N]<<endl;
        print(M,N);
        for (int i=0;i<(int)Ans.size()-1;i++)
            cout<<Ans[i]<<' ';
        cout<<Ans[Ans.size()-1]<<endl;
    }
    return 0;
}

C - C
Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

SGU 114
Description
Every city in Berland is situated on Ox axis. The government of the country decided to build new telecasting station. After many experiments Berland scientists came to a conclusion that in any city citizens displeasure is equal to product of citizens amount in it by distance between city and TV-station. Find such point on Ox axis for station so that sum of displeasures of all cities is minimal.

Input

Input begins from line with integer positive number N (0


C:

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
using namespace std;
const int MAX=15005;
const double MIN=0.0000001;
typedef long long ll;
struct city
{
    double x,num;
};
bool cmp(city a,city b)
{
    return a.x<b.x;
}
int N;
city cities[MAX];
double F(double X)
{
    double sum=0;
    for (int i=0;i<N;i++)
    {
        sum+=fabs(cities[i].x-X)*cities[i].num;
    }
    return sum;
}
int main()
{
    cout<<fixed<<setprecision(5);
    cin.sync_with_stdio(false);
    while (cin>>N)
    {
        for (int i=0;i<N;i++)
            cin>>cities[i].x>>cities[i].num;
        sort(cities,cities+N,cmp);
        double low=cities[0].x,high=cities[N-1].x,mid,midd;
        while (fabs(low-high)>MIN)
        {
            mid=(low+high)/2;
            midd=(mid+high)/2;
            if (F(mid)<F(midd))
                high=midd;
            else
                low=mid;
        }
        mid=(low+high)/2;
        cout<<mid<<endl;
    }
    return 0;
}

D - D
Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

SGU 139
Description
Little Johnny likes puzzles a lot! Just a few days ago, he found out about the ‘traditional’ 4x4 puzzle. For this puzzle, you have all the numbers from 0 to 15 arranged in 4 rows and 4 columns. You are allowed to switch two adjacent elements (horizontally or vertically), only if one of them has the value 0. The purpose of the puzzle is to reach the following final state:

                         1  2  3  4 
                         5  6  7  8 
                         9 10 11 12 
                        13 14 15  0

Given the initial state of the puzzle, you have to decide whether there exists a sequence of moves which brings the puzzle into the final state.

Input

The input will consist of 4 lines, each of them containing 4 integers, describing the initial state of the puzzle.

Output

For every initial state, you should print “YES” if the final state can be reached after several moves or “NO”, if such a thing is impossible.

Sample Input #1

1 2 3 4
5 6 7 8
9 10 11 0
13 14 15 12
Sample Output #1

YES
Sample Input #2

2 1 3 4
5 6 7 8
9 10 11 12
0 13 14 15
Sample Output #2

NO



D: 结论题 设状态p的总逆序对数与0到右下角的曼哈顿距离和为T(P),那么T(末状态)=0,因为每次移动对T的奇偶性没有影响,所以当T(初状态)为奇数是肯定无解,否则有解

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
using namespace std;
int main()
{
    int i,j,t=0,a[16];
    for(i=0;i<16;i++)
    {
        scanf("%d",&a[i]);
        if (!a[i])
            t=t+3-i/4;
        for(j=0;j<i;j++)
            if (a[i]&&(a[j]>a[i]))
                t++;
    }
    if (t&1)
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}

E - E
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 540B
Description
Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. Vova is very smart and he can write every test for any mark, but he doesn’t want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value x, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than y points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.

Vova has already wrote k tests and got marks a1, …, ak. He doesn’t want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.

Input
The first line contains 5 space-separated integers: n, k, p, x and y (1 ≤ n ≤ 999, n is odd, 0 ≤ k < n, 1 ≤ p ≤ 1000, n ≤ x ≤ n·p, 1 ≤ y ≤ p). Here n is the number of tests that Vova is planned to write, k is the number of tests he has already written, p is the maximum possible mark for a test, x is the maximum total number of points so that the classmates don’t yet disturb Vova, y is the minimum median point so that mom still lets him play computer games.

The second line contains k space-separated integers: a1, …, ak (1 ≤ ai ≤ p) — the marks that Vova got for the tests he has already written.

Output
If Vova cannot achieve the desired result, print “-1”.

Otherwise, print n - k space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.

Sample Input
Input
5 3 5 18 4
3 5 4
Output
4 1
Input
5 3 5 16 4
5 5 5
Output
-1
Hint
The median of sequence a1, …, an where n is odd (in this problem n is always odd) is the element staying on (n + 1) / 2 position in the sorted list of ai.

In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn’t exceed 18, that means that Vova won’t be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn’t less than 4, so his mom lets him play computer games.

Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: “4 2”, “2 4”, “5 1”, “1 5”, “4 1”, “1 4” for the first test is correct.

In the second sample Vova got three ‘5’ marks, so even if he gets two ‘1’ marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is “-1”.


E: ...比赛的时候一直没敢写这个 感觉每次都排序肯定超时..妈的竟然不超..


#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
using namespace std;
int n,k,p,x,y,A[1005];
vector<int> Ans;
int main()
{
    cin.sync_with_stdio(false);
    while (cin>>n>>k>>p>>x>>y)
    {
        Ans.clear();
        int sum=0;
        for (int i=1; i<=k; i++)
        {
            cin>>A[i];
            sum+=A[i];
        }
        sort(A+1,A+k+1);
        for (int i=k+1; i<=n; i++)
        {
            int mid=i/2;
            if (i%2==1)
                mid++;
            if (A[mid-1]>=y)
                A[i]=1;
            else
                A[i]=y;
            sum+=A[i];
            Ans.push_back(A[i]);
            sort(A+1,A+i+1);
        }
        if (sum>x||A[(n+1)/2]<y)
            cout<<-1<<endl;
        else
        {
            for (int i=0; i<(int)Ans.size(); i++)
                cout<<Ans[i]<<' ';
            cout<<endl;
        }
    }
    return 0;
}

F - F
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 5583
Description
In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.

Now N frogs are standing in a line, some of them are black, the others are white. The total strength of those frogs are calculated by dividing the line into minimum parts, each part should still be continuous, and can only contain one kind of frog. Then the strength is the sum of the squared length for each part.

However, an old, evil witch comes, and tells the frogs that she will change the color of at most one frog and thus the strength of those frogs might change.

The frogs wonder the maximum possible strength after the witch finishes her job.
Input
First line contains an integer T, which indicates the number of test cases.

Every test case only contains a string with length N, including only 0 (representing
a black frog) and 1 (representing a white frog).

\cdot 1 \leq T \leq 50.

\cdot for 60% data, 1 \leq N \leq 1000.

\cdot for 100% data, 1 \leq N \leq 10^5.

\cdot the string only contains 0 and 1.
Output
For every test case, you should output ” Case #x: y”,where x indicates the case number and counts from 1 and y is the answer.
Sample Input
2
000011
0101
Sample Output
Case #1: 26
Case #2: 10



F:


#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
const int MAX=100005;
ll A[MAX],temp;
int T,cnt;
char S[MAX];
int main()
{
    cin.sync_with_stdio(false);
    cin>>T;
    for (int cases=1; cases<=T; cases++)
    {
        cin>>(S+1);
        S[0]=S[1];
        int len=strlen(S+1);
        cnt=1;
        memset(A,0,sizeof(A));
        for (int i=1; i<=len; i++)
        {
            if (S[i]!=S[i-1])
                cnt++;
            A[cnt]++;
        }
        ll Ans=0;
        for (int i=1; i<=cnt; i++)
            Ans+=A[i]*A[i];
        ll Keep=Ans;
        for (int i=1; i<=cnt; i++)
        {
            if (A[i]==1)
            {
                temp=Keep-A[i]*A[i]-A[i-1]*A[i-1]-A[i+1]*A[i+1];
                temp+=(A[i-1]+A[i]+A[i+1])*(A[i-1]+A[i]+A[i+1]);
                Ans=max(Ans,temp);
            }
            else
            {
                temp=Keep-A[i]*A[i]-A[i-1]*A[i-1];
                temp+=(A[i-1]+1)*(A[i-1]+1)+(A[i]-1)*(A[i]-1);
                Ans=max(Ans,temp);
                temp=Keep-A[i]*A[i]-A[i+1]*A[i+1];
                temp+=(A[i+1]+1)*(A[i+1]+1)+(A[i]-1)*(A[i]-1);
                Ans=max(Ans,temp);
            }
        }
        cout<<"Case #"<<cases<<": "<<Ans<<endl;
    }
    return 0;
}
内容概要:本文深入探讨了Kotlin语言在函数式编程和跨平台开发方面的特性和优势,结合详细的代码案例,展示了Kotlin的核心技巧和应用场景。文章首先介绍了高阶函数和Lambda表达式的使用,解释了它们如何简化集合操作和回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建和平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发和Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程和跨平台开发领域不断完善和发展。; 适合人群:对函数式编程和跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者和中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数和Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解和实践Kotlin的函数式编程特性和跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解和掌握。
内容概要:本文深入探讨了利用历史速度命令(HVC)增强仿射编队机动控制性能的方法。论文提出了HVC在仿射编队控制中的潜在价值,通过全面评估HVC对系统的影响,提出了易于测试的稳定性条件,并给出了延迟参数与跟踪误差关系的显式不等式。研究为两轮差动机器人(TWDRs)群提供了系统的协调编队机动控制方案,并通过9台TWDRs的仿真和实验验证了稳定性和综合性能改进。此外,文中还提供了详细的Python代码实现,涵盖仿射编队控制类、HVC增强、稳定性条件检查以及仿真实验。代码不仅实现了论文的核心思想,还扩展了邻居历史信息利用、动态拓扑优化和自适应控制等性能提升策略,更全面地反映了群体智能协作和性能优化思想。 适用人群:具备一定编程基础,对群体智能、机器人编队控制、时滞系统稳定性分析感兴趣的科研人员和工程师。 使用场景及目标:①理解HVC在仿射编队控制中的应用及其对系统性能的提升;②掌握仿射编队控制的具体实现方法,包括控制器设计、稳定性分析和仿真实验;③学习如何通过引入历史信息(如HVC)来优化群体智能系统的性能;④探索中性型时滞系统的稳定性条件及其在实际系统中的应用。 其他说明:此资源不仅提供了理论分析,还包括完整的Python代码实现,帮助读者从理论到实践全面掌握仿射编队控制技术。代码结构清晰,涵盖了从初始化配置、控制律设计到性能评估的各个环节,并提供了丰富的可视化工具,便于理解和分析系统性能。通过阅读和实践,读者可以深入了解HVC增强仿射编队控制的工作原理及其实际应用效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值