有时候,很简单的模板题,可能有人没有做出来,(特指 I ),到时候一定要把所有的题目全部看一遍
文章目录
- B
- 题解
- E
- F
- 题解
- H
- I
- 题解&代码
- J
3 2
1 2 1
2 3 1
输出样例
1
说明
In the first sample case, the sequence [a1,a2,a3]=[0,1,0][a_1,a_2,a_3]=[0,1,0][a1,a2,a3]=[0,1,0] meets all the constraints and has the minimum sum of all the elements.
这一道题目的关键就是要知道:异或操作的操作是位与位之间相互独立的,所以就可以对于每一位进行单独考虑。
如果这一位异或操作的结果是1
那么就说明该这两位的取值必定相反,如果是0
,那么就说明这两位的取值必定相同。
#include
using namespace std;
typedef long long ll;
#define N 100020
int n, m;
//int a[N];
int fa[N*2];
struct {
int x, y, xo;
}b[N*2];
int get(int x)
{
if(fa[x] == x) return x;
return fa[x] = get(fa[x]);
}
ll solve(int idx)//对于每一位进行操作
{
for(int i = 1; i <= 2*n; i++) fa[i] = i;
for(int i = 1; i <= m; i++)
{
int x = b[i].x;
int y = b[i].y;
int xo = ((b[i].xo)>>idx)&1;
if(xo == 1){
if(get(x) == get(y) ){
return -1;
}
else
{
fa[get(x)] = get(y+n);
fa[get(y)] = get(x+n);
}
}
else if(xo == 0)
{
if(get(x) == get(y+n) ){
return -1;
}else{
fa[get(x)] = get(y);
fa[get(y+n)] = get(x+n);
}
}
}
map<int, pair<int, int> >mp;
for(int i = 1; i <= n; i++)
{
//cout << "n:" << n << "\n";
int id = min(get(i), get(i+n));
//cout << id << " " << get(i) << "\n";
if(get(i) == id){
mp[id].first++;
}
else {
mp[id].second++;
}
}
ll ans = 0;
for(auto &x : mp)
{
ans += min(x.second.first, x.second.second);
//cout << x.first << " "<< x.second.first << " " << x.second.second << "\n";
}
return ans;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &b[i].x, &b[i].y, &b[i].xo);
}
ll ans = 0;
for(int i = 0; i <= 30; i++)
{
ll ret;
ret = solve(i);
if(ret == -1){
puts("-1");
return 0;
}
ans += ret << i;
}
printf("%lld", ans);
system("pause");
return 0;
}
On November 6, 2021, the Chinese team Edward Gaming (EDG) defeated the South Korea team DWG KIA (DK) to win the 2021 League of Legends World championship in Reykjavík, Iceland, lifting the Summoner’s Cup for the first time in their history.
While both teams had looked dominant(占主导地位) throughout the competition, DK arguably(可以论证的) had the advantage. The team hadn’t lost a single game until they reached the semi-finals and was the only team to make it out of the Group Stage without a single defeat. They were clearly the team to beat.
EDG had given them a hit at the very first game of the final. The game started with a well-executed gank in the bot lane by EDG for the first blood. Later, EDG took every single Drake and the Baron, and ultimately destroyed the DK’s Nexus after 35 minutes.
But DK wouldn’t leave it unanswered. They maintained an advantage throughout the second game. Not even the incredible Baron steal by EDG’s legendary jungler, Jiejie , could help the team.
The third game turned out to be a difficult one. EDG seems to have control over more resources during the first 30 minutes. However, DK constantly killed every single dragon, and they finally took down the Nexus with the Hand of Baron.
In the fourth game, EDG had rethought their approach and took higher precedence in the control over dragons. The strategy had immediately taken effect, and they won the game after 33 minutes.
All things came down to the last game of the finals. Initially, DK took up the first dragon without much resistance from EDG. Shortly after, EDG picked first blood as DK took the Herald. Everything was fairly even at that moment. The balance finally started to tip in EDG’s favor during a team fight in the mid-lane, with EDG killing DK’s midlaner Showmaker before they had a chance to respond. The fight finally ended up with four kills and one death for EDG. They snowballed their advantage and finally secured the trophy.
The triumph of the Worlds 2021 made EDG the first team from LPL to win both the Mid-Season Invitational and the World Championship. You have just written a long string to celebrate this great victory. How many occurrences of “edgnb” as a continuous substring are there in the long string? Please write a program to count the number.
输入描述:
The only line contains a nonempty string, which consists of no more than 200000 lowercase Latin letters, ‘a’ to ‘z’.
输出描述:
Output a line containing a single integer, indicating the number of occurrences of “edgnb” in the given string.
edgnb
1
并没有必要阅读
#include
using namespace std;
char a[300000];
int main()
{
scanf("%s", a+1);
int len = strlen(a+1);
int ans = 0;
for(int i = 1; i <= len; i++)
{
if(a[i] == 'e' && a[i+1] == 'd' && a[i+2] == 'g' && a[i+3] == 'n' && a[i+4] == 'b')
ans ++;
}
cout << ans;
return 0;
}
输入样例1
4
aacc
输出样例1
bbaa
输入样例2
3
aca
输出样例2
ba
暴力枚举
#include
using namespace std;
#define N 1010
int n;
char a[N];
bool vis[128];
int cnt;
int ans[N];
int tmp[N];
int num[128];
bool check(int s)
{
int i = 1;
while(ans[i] == tmp[i] && i < N) i++;
if(tmp[i] > ans[i]) return true;
return false;
}
int main()
{
scanf("%d", &n);
scanf("%s", a+1);
for(int i = 1; i <= n; i++)
{
memset(vis, 0, sizeof(vis));
cnt = 0;
for(int j = i; j >= 1; j--)
{
if(!vis[a[j]]){
cnt++;
vis[a[j]] = true;
num[a[j]] = cnt;
}
tmp[j] = num[a[j]];
}
if(check(i)) {
for(int j = 1; j <= i; j++)
ans[j] = tmp[j];
}
// printf("DEBUG");
// for(int j = 1; j <= i; j++)
// {
// putchar('a'+tmp[j] - 1);
// }
// puts("");
}
int len = 1;
while(ans[len] != 0) len ++;
for(int i = 1; i < len; i++) putchar(ans[i]+'a'-1);
puts("");
system("pause");
return 0;
}
输入样例1
5 6
1 2 1
1 3 2
1 4 3
4 3 4
4 5 5
2 5 6
输出样例1
21
输入样例2
6 5
1 2 4
2 3 1
3 4 3
4 5 2
5 6 5
输出样例2
12
输入样例3
5 5
1 2 1
2 3 2
3 4 3
4 5 4
5 1 5
输出样例3
14
更为详细的题解见此处https://blog.csdn.net/li_wen_zhuo/article/details/121633394
常规的思维是先输入图G,然后通过手段构建出L(G),然后求图里面的最大匹配。
现在我们反过来,把要求带到G图中。
L(G)中的一条边就相当于是G中的具有相同端点的两条边。同时,所选择的边不重复。
经过转化之后,经过奇思妙想:
如果原图G中具有偶数条边,那么就可以选择所有的边。
证明:对于原图,是无环的,并且原图可以分为若干个连通块,每一个连通块连通并且无环,那么就是树。对于偶数个边的树:
(按照从叶子到根的方法进行考虑)
如果要是有一个节点具有偶数个儿子,那么这偶数个与儿子相连接的边两两组合。然后这一个节点变为叶子节点。
如果要是有一个节点具有奇数个儿子,那么这偶数个与儿子相连接的边两两组合。剩下的儿子,这一个节点,这一个节点的父亲所连接的边构成 具有相同端点的两条边
如果要是具有奇数条边,那么一定至少有一条边是无法进行匹配的。
删除一条边,按照常理所,剩下的就可以全部匹配了。但是可能这一条边恰恰是割边,并且删除这一条边以后形成的两个新的连通块中全部是奇数条边,那么就相当于有三条边无法匹配,所以损失过大,故是桥的这一条边还是不删为妙!
可以使用并查集来查看是否在同一个连通块里
由于对于每一个连通块都有可能出现有边无法匹配的情况,所以把边的权值从大到小进行排序(贪心),优先安排权值大的边。
注意:图论不一定是需要进行建图的,还可以仅仅存放点!
#include
using namespace std;
#define N 300020
int fa[N], val[N];//与并查集结合使用
int n, m;
struct edge{
int x, y, w;
bool operator <(const edge &o){//从大到小进行排列
return w > o.w;
}
}a[N];
int get(int x)
{
if(x == fa[x]) return x;
return fa[x] = get(fa[x]);//BUG1:没有写 fa[x] =
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].w);
}
sort(a+1, a+1+m);//BUG2:sort(a+1, a+1+n);
for(int i = 1; i <= n; i++){
fa[i] = i;
//val[i] = 0;如果是多组数据,就需要!
}
long long ans = 0;
for(int i = 1; i <= m; i++)
{
int x = get(a[i].x), y = get(a[i].y), w = a[i].w;
if(x == y){
if(val[x]){
ans += val[x] + w;
val[x] = 0;
}else{
val[x] = w;
}
}
else {//x != y
if(val[x] && val[y])//两边都是奇数,要选这一条边
{
ans += w + max(val[x], val[y]);
fa[x] = y;
val[y] = min(val[x], val[y]);
}
else if(!val[x] && !val[y])//两边是偶数,可以选这一条边,也可以不选。但是边是按照从大到小来进行排序的,所以暂时不选这一条边是最好的策略
{
fa[x] = y;
val[y] = w;
}
else {
ans += max(val[x], val[y])+w;
fa[x] = y;
val[y] = 0;
}
}
}
cout << ans;
return 0;
}
输入样例
2
-1 0 0 -1
0 1 -1 0
1 0 0 1
0 -1
-1 0 -1 0
0 1 0 -1
1 0 1 0
0 -1
输出样例
1.000000000000000 0.000000000000000
0.000000000000000 1.000000000000000
In the first sample case we have$ f(z)=iz$, and in the second sample case we have f ( z ) = 1 / z f(z)=1/z f(z)=1/z.
没有什么好说的,敲就完了!
但是要注意一点:C++里面本来就有 复数这一个东西!
但是C++里面的复数的等于号有一点点阴间,所以等于根据精度判断一下就好了
#include
#define ll long long
#define PII pair<int,int>
const double eps = 1e-6;
using namespace std;
class Complex{
public:
double x, y;
Complex(){
x = 0;
y = 0;
}
Complex(double a, double b){
x = a;
y = b;
}
};
Complex operator + (const Complex &a, const Complex &b) {
return Complex( a.x + b.x, a.y + b.y );
}
Complex operator - (const Complex &a, const Complex &b) {
return Complex( a.x - b.x, a.y - b.y );
}
Complex operator * (const Complex &a, const Complex &b) {
return Complex( a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);
}
Complex operator / (const Complex &a, const Complex &b) {
return Complex( (a.x*b.x+a.y*b.y)/(b.x*b.x + b.y*b.y), (a.y*b.x-a.x*b.y)/(b.x*b.x + b.y*b.y) );
}
bool operator == (const Complex &a, const Complex &b) {
if(fabs(a.x-b.x) < eps && fabs(a.y-b.y) < eps) return true;
else return false;
}
Complex c[5][5], b[5];
int main()
{
int T;
cin >> T;
while(T--)
{
double tx, ty;
Complex z0, z1, z2, z3, w1, w2, w3, w0;
scanf("%lf%lf", &tx, &ty);
z1 = Complex(tx, ty);
scanf("%lf%lf", &tx, &ty);
w1 = Complex(tx, ty);
scanf("%lf%lf", &tx, &ty);
z2 = Complex(tx, ty);
scanf("%lf%lf", &tx, &ty);
w2 = Complex(tx, ty);
scanf("%lf%lf", &tx, &ty);
z3 = Complex(tx, ty);
scanf("%lf%lf", &tx, &ty);
w3 = Complex(tx, ty);
scanf("%lf%lf", &tx, &ty);
z0 = Complex(tx, ty);
Complex m, n;
m = (w1-w2)/(z1-z2);
n = (w1-m*z1);
if(m*z3+n == w3){//如果分母为0
w0 = m*z0 + n;
printf("%.15lf %.15lf\n", w0.x, w0.y);
continue;
}
Complex zero(0, 0);
c[1][1] = z1, c[1][2] = zero - w1, c[1][3] = Complex(1, 0);
c[2][1] = z2, c[2][2] = zero - w2, c[2][3] = Complex(1, 0);
c[3][1] = z3, c[3][2] = zero - w3, c[3][3] = Complex(1, 0);
b[1] = z1*w1;
b[2] = z2*w2;
b[3] = z3*w3;
//高斯的错
for(int i = 1; i <= 3; i++)
{
for(int j = i; j <= 3; j++)
{
if(!(c[j][i]==zero)){
for(int k = 1; k <= 3; k++) swap(c[i][k], c[j][k]);
swap(b[i], b[j]);
}
}
for(int j = 1; j <= 3; j++){
if(i == j) continue;
Complex rate = c[j][i] / c[i][i];
for(int k = i; k <= 3; k++)
{
c[j][k] = c[j][k] - c[i][k] * rate;
}
b[j] = b[j] - b[i] * rate;
}
}
Complex ans = (b[1]/c[1][1]*z0 + b[3]/c[3][3])/(z0+(b[2]/c[2][2]));
printf("%.15lf %.15lf\n", ans.x, ans.y);
}
system("pause");
return 0;
}
输入样例
6
1234 2345
1234 0123
1234 2267
1234 3401
1234 1344
1234 2468
输出样例
1
1
4
5
1
4
这一道题目我们对先采用了大模拟,发现考虑的情况太多,然后又采用了暴力搜索。
参考题解:https://zhuanlan.zhihu.com/p/563484871
第 J 题
就如其中所说的,如果每一次都进行暴力,那么就有可能超时。
所以需要转变一下思路。
#include
using namespace std;
map<string, int> mp;//其实使用数组也可以,但是把一个字符串映射为一个整数比较麻烦,so
queue<string> q;
void bfs()
{
q.push("0000");
mp["0000"] = 0;
while(!q.empty())
{
string s = q.front();
q.pop();
//if(mp.count(s)) continue;
for(int i = 0; i < 4; i++)
{
for(int j = i; j < 4; j++)
{
string up = s;
string down = s;
for(int k = i; k <= j; k++)
{
up[k] = (up[k]-'0'+1)%10+'0';
down[k] = (down[k]-'0'-1+10)%10+'0';
}
if(!mp.count(up)) {
mp[up] = mp[s] + 1;
q.push(up);
}
if(!mp.count(down)) {
mp[down] = mp[s] + 1;
q.push(down);
}
}
}
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);//浅浅优化一下
//但是在这样搞了以后,就不要使用printf()了,可能导致输出顺序不一样然后寄掉
bfs();
int T;
cin >> T;
while(T--)
{
string a, b;
cin >> a >> b;
for(int i = 0; i < 4; i++){
b[i] = ((a[i]-'0')-(b[i]-'0')+10)%10+'0';
}
cout << mp[b] << "\n";
}
return 0;
}