Codeforces Round #277 (Div. 2) 題解



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
4
output
2
input
5
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 2
1 0
0 0
output
NO
input
2 3
1 1 1
1 1 1
output
YES
1 1 1
1 1 1
input
2 3
0 1 0
1 1 1
output
YES
0 0 0
0 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 3
aeabcaez
output
6
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'的中間值上下移動的次數是一樣的,所以沒有必要跨越中點

僅僅要計算出在一邊的左右移動次數就能夠了....

#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 <----> Rif(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 4
2 1 3 2
1 2
1 3
3 4
output
8
input
0 3
1 2 3
1 2
2 3
output
3
input
4 8
7 8 7 5 4 6 4 10
1 6
1 2
5 8
1 3
3 5
6 7
3 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]<=root+D 之內的點, DP[u]= 全部子節點(DP[v]+1)相乘?

假設擴展到某個節點 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?<?i2?<?...?<?ik?≤?n?is called increasing if?ai1?<?ai2?<?ai3?<?...?<?aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

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
1
4
output
3
input
4
1 3 2 5
output
3223
input
4
1 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?<?l, otherwise:

    2) if?Fi?=?l

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

  • How to caculate?Fi? We have:?Fi?=?max{F1u?+?F2v}?among?1?≤?u?<?i?<?v?≤?n?such that?au?<?av. From this formula, we can use Segment tree to calculate?Fi. Due to limitation of my English, it is really hard to write exactly how. I will post my code soon.


正反求兩遍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;
}





轉載于:https://www.cnblogs.com/blfshiye/p/4295647.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376207.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376207.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376207.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

QT 邊框圓角處理

平時的邊框是平角的&#xff1a; 如果需要圓角的話&#xff0c;就要加stylesheet加上這個&#xff1a; border-radius:3px;比如&#xff1a; QPushButton{ border-radius:3px; }就變成圓角了&#xff1a; px前面的數字越大就越圓&#xff0c;比如5px比3px圓 假如只需要某一…

3級調度 fpga_Vivado HLS學習筆記——1.了解FPGA架構

本篇文章為本人學習Xilinx的Vivado HLS教程記錄的學習筆記&#xff0c;僅供學習參考。Vivado HLS官方視頻教程&#xff1a;優酷視頻?v.youku.com目錄&#xff1a; Vivado HLS課程簡介FPGA與CPU、GPU、DSP的區別FPGA的優勢Xilinx FPGA架構:邏輯單元、算術邏輯單元、存儲單元使用…

[LeetCode]Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思考&#xff1a;DFS。 /*** Definition for binary tree* struct TreeNode {* int val;* Tree…

BZOJ2435 [Noi2011]道路修建

這是NOI11年題&#xff0c;你在逗我&#xff1f; 直接dfs就可以了&#xff0c;Linux下貌似不會爆棧。。。 1 /**************************************************************2 Problem: 24353 User: rausen4 Language: C5 Result: Accepted6 Time:5184 …

Qt異常結束程序無法重新運行

有時候代碼有問題會導致qt異常結束 修改完后重新運行又會出現 查看任務管理器又沒有這個進程 可以使用資源管理器打開看看 也可以考慮使用process explorer查看 發現程序掛起來&#xff0c;結束掉它就可以重新運行了

hadooppythonsql_半小時搞定Hadoop+Mysql+Hive+Python

1. 說明搭建過Hadoop集群的小伙伴一定知道&#xff0c;如果不用docker&#xff0c;半小時配好HadoopMysqlHive(后簡稱Hive)肯定是胡吹&#xff0c;有了Docker鏡像&#xff0c;沒有說明文檔&#xff0c;配好了也不一定會用。本文將介紹如何在半小時內&#xff0c;讓Hive在你的Li…

PHP 切割字符串 點號 不用雙斜杠

$name "tupian.png"; $nameArr explode(".", $name); 習慣了Java的程序員容易寫成 $nameArr explode("\\.", $name);//在PHP中是不正確的轉載于:https://www.cnblogs.com/wuyou/p/3463425.html

Qt新添加的類無法鏈接

通過這個方法給工程添加了個類&#xff1a; 編譯的時候就出現了這個問題&#xff1a; 執行一下qmake 然后再重新構建項目就可以了

URAL 1830 Help in the RNOS 思路,讀題 難度:1

http://acm.timus.ru/problem.aspx?space1&num1830 這道題需要理解題目操作的意思, 要更改第i位的狀態,第i-1位必須激活為1,0-i-2位必須為0,如果0-i-1位開始時全為0,那么從0位開始進行操作 一.首先考慮對于0-i-1位都是0,需要更改i位的情況,需要 1.更改i-1位,2.按一下打開下…

openssh升級sftp_OpenSSH 8.2 發布 包括 sftp 客戶端和服務器支持

OpenSSH 8.2 發布了。OpenSSH 是 100% 完整的 SSH 協議 2.0 版本的實現&#xff0c;并且包括 sftp 客戶端和服務器支持。此版本變化不少&#xff0c;其中有兩個地方值得特別關注。一個是新特性&#xff0c;此版本增加了對 FIDO/U2F 硬件身份驗證器的支持。FIDO/U2F 是廉價硬件雙…

任務隊列摘自新鋒

在開發C程序時&#xff0c;一般在吞吐量、并發、實時性上有較高的要求。設計C程序時&#xff0c;總結起來可以從如下幾點提高效率&#xff1a; l 并發l 異步l 緩存下面將我平常工作中遇到一些問題例舉一二&#xff0c;其設計思想無非以上三點。 1任務隊列 1.1 以生產者-消…

C++容器遍歷時刪除元素

vector 錯誤做法 這樣做會在遍歷過程中越界導致程序崩潰 std::vector<int> vecInt({ 1, 3, 2, 1, 4, 1 });for (auto i vecInt.begin(); i ! vecInt.end() ; i) {if (*i 1) {vecInt.erase(i);}}正確做法 std::vector<int> vecInt({ 1, 3, 2, 1, 4, 1 });for (a…

按鈕圖片拉伸_圖片墻有多香?高手都在用的PPT封面制作技巧!

大家好&#xff0c;我是李導~這次&#xff0c;冬天是真的來了&#xff0c;不知道大家有沒有感覺&#xff0c;每次冷空氣真正襲來之前我們都會以為今年是個暖冬&#xff0c;結果突然有一天氣溫從20度直降到個位數&#xff0c;我們都會認為今年比以往的冬天都冷。但是&#xff0c…

POJ 1745 Divisibility【DP】

題意&#xff1a;給出n,k,n個數&#xff0c;在這n個數之間任意放置,-號&#xff0c;稱得到的等式的值能夠整除k則為可劃分的&#xff0c;否則為不可劃分的。 自己想的是枚舉&#xff0c;將所有得到的等式的和算出來&#xff0c;再判斷它是否能夠整除k&#xff0c;可是有10000個…

三種root的修補方式

三種root的修補方式 system/core/adb/abd.c adbd漏洞&#xff0c;請看abd.c的第917行/* then switch user and group to "shell" */ if (setgid(AID_SHELL) ! 0) { exit(1); } if (setuid(AID_SHELL) ! 0) { exit(1); …

數據挖掘十大經典算法

國際權威的學術組織the IEEE International Conference on Data Mining (ICDM) 2006年12月評選出了數據挖掘領域的十大經典算法&#xff1a;C4.5, k-Means, SVM, Apriori, EM, PageRank, AdaBoost, kNN, Naive Bayes, and CART. 不不過選中的十大算法&#xff0c;事實上參加評選…

windows dmp文件為0kb

列出一些遇到的情況提供參考&#xff1a; 1、棧溢出&#xff0c;多次調用T2A函數會出現程序崩潰但是dmp為0kb的問題。

dynamic與var

dynamic與var示例 var是一種語法省略寫法&#xff0c;編譯器會根據上下文推斷出正確的類型。 int[] scores new int[] { 1, 2, 7, 9, 8, 4, 6, 5 };foreach (var item in scores){Console.WriteLine(item);} 在大多數情況下&#xff0c;dynamic 類型與 object 類型的行…

線程間的消息(或數據)傳遞

使用“事件”可以實現線程間“消息/數據”的傳遞&#xff0c;非常棒的一種方法。轉載于:https://www.cnblogs.com/changbaishan/p/3471113.html

gt9xx linux 移植_GT9XX驅動移植說明書_for_Android_2014011401.pdf

GT9XXforAndroid驅動移植說明書一、驅動基本信息支持芯片型號 GT911 GT9110 GT9110P GT913 GT915 GT918 GT927 GT928 GT960GT968 GT910 GT912 GT960F GT950 GT968F GT9158 GT967 GT9150GT963GT9271GT917DI2C設備地址(7位) 0x5d、0x14I2C寄存器地址 16位APK工具/ADB工具 支持自動…