0

0

Codeforces Round #277 (Div. 2) 题解_html/css_WEB-ITnose

php中文网

php中文网

发布时间:2016-06-24 11:54:18

|

1181人浏览过

|

来源于php中文网

原创



Codeforces Round #277 (Div. 2)

A. Calculating Function

time limit per test

1 second

memory limit per test

256 megabytes

立即学习前端免费学习笔记(深入)”;

input

standard input

output

standard output

For a positive integer n let's define a function f:

f(n)?=??-?1?+?2?-?3?+?..?+?(?-?1)nn

Your task is to calculate f(n) for a given integer n.

Input

The single line contains the positive integer n (1?≤?n?≤?1015).

Output

Print f(n) in a single line.

Sample test(s)

input

output

input

output

-3

Note

f(4)?=??-?1?+?2?-?3?+?4?=?2

f(5)?=??-?1?+?2?-?3?+?4?-?5?=??-?3

简单公式:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long int LL;LL n;int main(){    cin>>n;    if(n%2==0)    {        LL t=n/2;        cout<<t<<endl;    }    else    {        LL t=(n-1)/2;        cout<<t-n<<endl;    }    return 0;}


B. OR in Matrix

time limit per test

1 second

memory limit per test

256 megabytes

立即学习前端免费学习笔记(深入)”;

input

standard input

output

standard output

Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0,?1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:

 where  is equal to 1 if some ai?=?1, otherwise it is equal to 0.

Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i(1?≤?i?≤?m) and column j (1?≤?j?≤?n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:

.

(Bij is OR of all elements in row i and column j of matrix A)

Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.

Input

The first line contains two integer m and n (1?≤?m,?n?≤?100), number of rows and number of columns of matrices respectively.

The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).

Output

In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.

Sample test(s)

input

2 21 00 0

output

NO

input

2 31 1 11 1 1

output

YES1 1 11 1 1

input

2 30 1 01 1 1

output

YES0 0 00 1 0

水题:将A里所有可能是1的点加上就可以了

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m;int B[220][220];int A[220][220];bool vis[220][220];bool check(int x,int y){    bool flag=true;    for(int i=0;i<m&&flag;i++)    {        if(vis[x][i]==false&&B[x][i]==0) flag=false;    }    for(int i=0;i<n&&flag;i++)    {        if(vis[i][y]==false&&B[i][y]==0) flag=false;    }    return flag;}void CL(int x,int y){    for(int i=0;i<m;i++)        vis[x][i]=true;    for(int i=0;i<n;i++)        vis[i][y]=true;}int main(){    cin>>n>>m;    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)            cin>>B[i][j];    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(check(i,j))///ONE            {                A[i][j]=1;                CL(i,j);            }        }    }    bool flag=true;    for(int i=0;i<n&&flag;i++)        for(int j=0;j<m&&flag;j++)            if(B[i][j]==1&&vis[i][j]==0) flag=false;    if(flag)    {        puts("YES");        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)                cout<<A[i][j]<<" ";            cout<<endl;        }    }    else puts("NO");    return 0;}



C. Palindrome Transformation

time limit per test

1 second

memory limit per test

256 megabytes

立即学习前端免费学习笔记(深入)”;

input

standard input

output

standard output

Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1?≤?i?≤?n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i?-?1 if i?>?1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i?=?n, the cursor appears at the beginning of the string).

When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.

Initially, the text cursor is at position p.

Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

Input

The first line contains two space-separated integers n (1?≤?n?≤?105) and p (1?≤?p?≤?n), the length of Nam's string and the initial position of the text cursor.

The next line contains n lowercase characters of Nam's string.

Output

Print the minimum number of presses needed to change string into a palindrome.

Sample test(s)

input

8 3aeabcaez

output

Note

A string is a palindrome if it reads the same forward or reversed.

In the sample test, initial Nam's string is:  (cursor position is shown bold).

In optimal solution, Nam may do 6 following steps:

The result, , is now a palindrome.


因为没有删除操作,最后的回文串是什么样已经是确定的了, A-->A'  或 A'--->A 或到A和A'的中间值上下移动的次数是一样的,所以没有必要跨越中点

只要计算出在一边的左右移动次数就可以了....

Dang.ai
Dang.ai

Dang.ai是一个AI工具目录集,已收集超过5000+ AI工具

下载

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn=200100;const int INF=0x3f3f3f3f;char str[maxn];char rstr[maxn];int n,p;int change[maxn][2];void getC(){    for(int i=0;i<n;i++)    {        /// 0: A --> A'        change[i][0]=max(str[i],rstr[i])-min(str[i],rstr[i]);        change[i][0]=min(change[i][0],min(str[i],rstr[i])+26-max(str[i],rstr[i]));        /// 1: A --> m <-- A'        change[i][1]=change[i][0];    }}int main(){    cin>>n>>p;    p--;    cin>>str;    int tt=n/2;    if(n%2==0) tt--;    if(p>tt)    {        reverse(str,str+n);        p=n-1-p;    }    memcpy(rstr,str,sizeof(str));    reverse(rstr,rstr+n);    getC();    int temp=0;    ///改变字符的花费    for(int i=0;i<=tt;i++)    {        temp+=change[i][0];    }    ///移动的花费    ///需要改变的左右边界    int R=-1;    for(int i=tt;i>=0;i--)    {        if(change[i][0]!=0)        {            R=i; break;        }    }    int L=-1;    for(int i=0;i<=tt;i++)    {        if(change[i][0]!=0)        {            L=i; break;        }    }    if(L==-1||R==-1)    {        puts("0");    }    else if(L==R)    {        cout<<abs(p-L)+temp<<endl;    }    else    {        /// L <----> R        if(p>=L&&p<=R)        {            int t=min(abs(R-p),abs(L-p));            cout<<R-L+t+temp<<endl;        }        else if(p<L)        {            cout<<abs(R-p)+temp<<endl;        }        else if(p>R)        {            cout<<abs(L-p)+temp<<endl;        }    }    return 0;}



D. Valid Sets

time limit per test

1 second

memory limit per test

256 megabytes

立即学习前端免费学习笔记(深入)”;

input

standard input

output

standard output

As you know, an undirected connected graph with n nodes and n?-?1 edges is called a tree. You are given an integer d and a tree consisting of nnodes. Each node i has a value ai associated with it.

We call a set S of tree nodes valid if following conditions are satisfied:

  1. S is non-empty.
  2. S is connected. In other words, if nodes u and v are in S, then all nodes lying on the simple path between u and v should also be presented in S.
  3. .

Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007 (109?+?7).

Input

The first line contains two space-separated integers d (0?≤?d?≤?2000) and n (1?≤?n?≤?2000).

The second line contains n space-separated positive integers a1,?a2,?...,?an(1?≤?ai?≤?2000).

Then the next n?-?1 line each contain pair of integers u and v (1?≤?u,?v?≤?n) denoting that there is an edge between u and v. It is guaranteed that these edges form a tree.

Output

Print the number of valid sets modulo 1000000007.

Sample test(s)

input

1 42 1 3 21 21 33 4

output

input

0 31 2 31 22 3

output

input

4 87 8 7 5 4 6 4 101 61 25 81 33 56 73 4

output

41

Note

In the first sample, there are exactly 8 valid sets: {1},?{2},?{3},?{4},?{1,?2},?{1,?3},?{3,?4} and {1,?3,?4}. Set {1,?2,?3,?4} is not valid, because the third condition isn't satisfied. Set {1,?4} satisfies the third condition, but conflicts with the second condition.


树型DP,从每一个节点走只扩展和根节点 root  权值 在 root

如果扩展到某个节点 w[v]==w[root]   则标记一下,不要重复走

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>using namespace std;typedef long long int LL;const int maxn=2222;const LL mod=1000000007;int n,d,root;LL w[maxn];vector<int> g[maxn];bool vis[maxn][maxn];LL dp[maxn];LL dfs(int u,int fa){    dp[u]=1;    for(int i=0,sz=g[u].size();i<sz;i++)    {        int v=g[u][i];        if(v==fa) continue;        if(!((w[root]<=w[v])&&(w[v]<=w[root]+d))) continue;        if(vis[root][v]) continue;        if(w[root]==w[v]) vis[root][v]=vis[v][root]=true;        int temp=dfs(v,u);        dp[u]=(dp[u]+temp*dp[u])%mod;    }    return dp[u];}int main(){    cin>>d>>n;    for(int i=1; i<=n; i++)        cin>>w[i];    for(int i=0; i<n-1; i++)    {        int a,b;        cin>>a>>b;        g[a].push_back(b);        g[b].push_back(a);    }    LL sum=0;    for(int i=1; i<=n; i++)    {        root=i;        sum=(sum+dfs(i,i))%mod;    }    cout<<sum<<endl;    return 0;}


E. LIS of Sequence

time limit per test

2 seconds

memory limit per test

256 megabytes

立即学习前端免费学习笔记(深入)”;

input

standard input

output

standard output

The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.

Nam created a sequence a consisting of n (1?≤?n?≤?105) elements a1,?a2,?...,?an (1?≤?ai?≤?105). A subsequence ai1,?ai2,?...,?aik where 1?≤?i1?

Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1?≤?i?≤?n), into three groups:

  1. group of all i such that ai belongs to no longest increasing subsequences.
  2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
  3. group of all i such that ai belongs to every longest increasing subsequence.

Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.

Input

The first line contains the single integer n (1?≤?n?≤?105) denoting the number of elements of sequence a.

The second line contains n space-separated integers a1,?a2,?...,?an (1?≤?ai?≤?105).

Output

Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.

Sample test(s)

input

14

output

input

41 3 2 5

output

3223

input

41 5 2 3

output

3133

Note

In the second sample, sequence a consists of 4 elements: {a1,?a2,?a3,?a4} = {1,?3,?2,?5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1,?a2,?a4} = {1,?3,?5} and {a1,?a3,?a4} = {1,?2,?5}.

In the third sample, sequence a consists of 4 elements: {a1,?a2,?a3,?a4} = {1,?5,?2,?3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1,?a3,?a4} = {1,?2,?3}.



Solution 2:

// Some notation is re-defined.

  • Let F1i be the length of LIS ending exactly at ai of sequence {a1,?a2,?...,?ai}.

  • Let F2i be the length of LIS beginning exactly at ai of sequence {ai,?ai?+?1,?...,?an}.

  • l = length of LIS of {a1,?a2,?...,?an} = max{F1i} = max{F2j}.

  • Let Fi be the length of LIS of sequence {a1,?a2,?...,?ai?-?1,?ai?+?1,?...,?an} (i.e the length of LIS of initial sequence a after removing element ai).

  • Index i must in group:

    1) if F1i?+?F2i?-?1?

    2) if Fi?=?l

    3) if Fi?=?l?-?1

  • How to caculate Fi? We have: Fi?=?max{F1u?+?F2v} among 1?≤?u?


  • 正反求两遍LIS,比较一下即可.....


    如果F1[i]+F2[j]-1==LIS 要用map记录下有没有相同的F1[i],F2[i]   有输出2 没有输出3


    #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <set>#include <map>using namespace std;const int maxn=100100;int n,a[maxn],b[maxn];int f1[maxn],f2[maxn];int v1[maxn],n1,v2[maxn],n2;set<int> st;map<pair<int,int>,int> mp;int r[maxn],rn;int ans[maxn];int main(){    scanf("%d",&n);    for(int i=0;i<n;i++)    {        scanf("%d",a+i);        r[rn++]=a[i];    }    sort(r,r+rn);    rn=unique(r,r+rn)-r;    ///.....rhash.....    for(int i=0;i<n;i++)    {        int id=lower_bound(r,r+rn,a[i])-r;        id=rn-1-id;        b[n-1-i]=r[id];    }    int LIS=1;    for(int i=0;i<n;i++)    {        if(i==0)        {            v1[n1++]=a[i];            v2[n2++]=b[i];            f1[0]=f2[0]=1;        }        else        {            int p1=lower_bound(v1,v1+n1,a[i])-v1;            v1[p1]=a[i];            if(p1==n1) n1++;            f1[i]=p1+1;            LIS=max(LIS,f1[i]);            int p2=lower_bound(v2,v2+n2,b[i])-v2;            v2[p2]=b[i];            if(p2==n2) n2++;            f2[i]=p2+1;        }    }    for(int i=0;i<n;i++)    {        int x=i,y=n-1-i;        if(f1[x]+f2[y]-1<LIS)            ans[i]=1;        else if(f1[x]+f2[y]-1==LIS)        {            ans[i]=4;            mp[make_pair(f1[x],f2[y])]++;        }    }    for(int i=0;i<n;i++)    {        if(ans[i]==4)        {            int x=i,y=n-1-i;            if(mp[make_pair(f1[x],f2[y])]==1) ans[i]=3;            else ans[i]=2;        }        printf("%d",ans[i]);        if(i==n-1) putchar('\n');    }    return 0;}





    相关文章

    HTML速学教程(入门课程)
    HTML速学教程(入门课程)

    HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

    下载

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热门AI工具

    更多
    DeepSeek
    DeepSeek

    幻方量化公司旗下的开源大模型平台

    豆包大模型
    豆包大模型

    字节跳动自主研发的一系列大型语言模型

    通义千问
    通义千问

    阿里巴巴推出的全能AI助手

    腾讯元宝
    腾讯元宝

    腾讯混元平台推出的AI助手

    文心一言
    文心一言

    文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

    讯飞写作
    讯飞写作

    基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

    即梦AI
    即梦AI

    一站式AI创作平台,免费AI图片和视频生成。

    ChatGPT
    ChatGPT

    最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

    相关专题

    更多
    pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
    pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

    本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

    797

    2026.02.13

    微博网页版主页入口与登录指南_官方网页端快速访问方法
    微博网页版主页入口与登录指南_官方网页端快速访问方法

    本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

    272

    2026.02.13

    Flutter跨平台开发与状态管理实战
    Flutter跨平台开发与状态管理实战

    本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

    144

    2026.02.13

    TypeScript工程化开发与Vite构建优化实践
    TypeScript工程化开发与Vite构建优化实践

    本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

    25

    2026.02.13

    Redis高可用架构与分布式缓存实战
    Redis高可用架构与分布式缓存实战

    本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

    92

    2026.02.13

    c语言 数据类型
    c语言 数据类型

    本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

    53

    2026.02.12

    雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
    雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

    本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

    15

    2026.02.12

    豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
    豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

    本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

    717

    2026.02.12

    PostgreSQL性能优化与索引调优实战
    PostgreSQL性能优化与索引调优实战

    本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

    64

    2026.02.12

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号