Codeforces Global Round 6
GsjzTle 人气:0久违的写篇博客吧
A. Competitive Programmer
题目链接:https://codeforces.com/contest/1266/problem/A
题意:
给你一个只包含数字的字符串,你可以将字符串随机排列,问最后组成的数字能否被60整除
分析:
我们先考虑能被6整除的数的特点:
①各个位数和 % 3 == 0
②末尾数必须为 0 2 4 6 8
再考虑能被10整除的数的特点:
末尾数必须为0
因为60中6在十位部分 ,10在个位部分,所以最后组成的数除了各个位数和 % 3 == 0 外十位部分必须为0 、2 、4、6、8 , 个位数必须为0。
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 1e3 + 10; ll a[N][N]; map<int , int>ha; int main() { ios; int n ; cin >> n; while(n --) { ha.clear(); string s; cin >> s; int len = s.size() - 1; int tot = 0; rep(j , 0 , len) { ha[s[j] - '0'] ++; tot += s[j] - '0'; } if(ha[0] && (ha[2] || ha[4] || ha[6] || ha[8] || (ha[0] - 1)) && tot % 3 == 0) cout << "red" << '\n'; else cout << "cyan" << '\n'; } return 0; }
B. Dice Tower
题目链接:https://codeforces.com/contest/1266/problem/B
题意:
你有无限个骰子,你可以从中选出任意个来拼搭,问“没有被挡住的点数的和”能否组成某个数X
分析:
首先一个骰子所有点数和为21 , 并且1的对立面为6,2的对立面为5,3对立面为4。
当骰子只有一个的时候,它被挡住的只会它的底部,那么它可以组成的数就为21 - (1~6) = 15 ~ 20
当骰子有两个的时候,因为它们要拼搭在一起,所以第一个骰子会被第二个骰子挡住一个面,第二个骰子会被第一个骰子挡住一个面,并且这两个面是对立的
所以当第二个骰子加入的时候,总的点数为原先点数 + 21(第二个骰子的总点数) - 7(第二个骰子加入时减去的部分) = (15 ~ 20) + 14
加入第三个第四个第五个。。。第n个同理 , 最后的总点数就为 (15 ~ 20) + 14 * (n - 1) , 所以如果可以拼凑出X则 X % 14 的答案为 (15 ~ 20 )% 14 = 1 ~ 6
所以最后只要判断X是否大于等于15并且 X % 14 在不在 1 ~ 6 这个范围就可以了
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 1e3 + 10; ll a[N][N]; map<int , int>ha; int main() { ios; int t; cin >> t; while(t --) { ll x; cin >> x; if(x < 15) { cout << "NO" << '\n'; continue; } x %= 14; if(x > 6 || x < 1) cout << "NO" << '\n'; else cout << "YES" << '\n'; } return 0; }
C. Diverse Matrix
题目链接:https://codeforces.com/contest/1266/problem/C
题意:
要求构造一个 r 行 c 列的矩阵, 并且矩阵的每一行的gcd和每一列的gcd的都只出现过一次,且每行的gcd都要求最小
分析:
首先当 r = 1 && c = 1 的时候, 它的行gcd和列gcd为同一元素, 所以是无法构造出来的
当 r = 1 或者 c = 1 时 , 我们可以 1 2 3 4 5 6 ... 输出
对于其它的可能,我们考虑这么构造:
假设 r = 2 , c = 2 , A 、B 、C 、D 为矩阵元素
1 | A C
2 | B D 我们可以让第一行的gcd结果为1 , 第二行gcd结果为2,第一列gcd结果为3,第二列gcd结果为4
3 4
则 A = 1 * 3 , B = 2 * 3 , C = 1 * 4 , D = 2 * 4 这样构造即可
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 1e3 + 10; ll a[N][N]; int main() { ios; ll r , c; cin >> r >> c; if(r == 1 && c == 1) return cout << 0 << '\n' , 0; if(r == 1) { int tot = 2; rep(i , 1 , c) cout << tot ++ << ' '; return 0; } if(c == 1) { int tot = 2; rep(i , 1 , r) cout << tot ++ << "\n"; return 0; } rep(i , 1 , r) { rep(j , 1 , c) { a[i][j] = i * (j + r); } } rep(i , 1 , r) { rep(j , 1 , c) cout << a[i][j] << " "; cout << '\n'; } return 0; }
D. Decreasing Debts
题目链接:https://codeforces.ml/contest/1266/problem/D
题意:
给你几个债务关系,如A欠B五元, B欠C五元,此时从的债务为 5 + 5 = 10 。
我们可以把关系转换为A欠C五元,那这样总的债务为 5
问不限转换次数 , 怎么把总债务化为最小。
分析:
当把总债务化为最小时 , 每个人的债务关系也将是最简的(以上述例子阐述,对B , 它欠C五元,这时候它的支出为5,但是A欠它五元,所以它的收入为5,所以它最后对总债务的关系为abs(5 - 5)= 0)
对于A、C也是一样的,所以最后A的最简形式为A支出5元,B的最简形式为0,C的最简形式为收入5元。按照这样,我们只要将A指向C即可
(因为总的收入和总的支出一定是相同的且题目也说债务关系数量最小,所以即使某个人 D 收入的金额大于另一个人 E 支出的金额,也可以先将E连向D,然后D的收入金额减去E的支出金额)
我们用 in 容器来存最简形式为收入的人的编号, ou容器来存最简形式为支出的人的编号,再用指针one指向in里的元素,two指向ou里的元素,他们的债务可以表示为min(a[one] , a[two]) , 将关系存于ans容器里。
当one == in.size() || two == ou.size() 即已经没有支出的人或已经没有收入的人了,则债务关系可以结束储存(题目要求最后输出债务关系)
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 3e5 + 10; ll a[N]; vector<ll>in , ou; vector<pair<pair<ll , ll>, ll> > ans; int main() { ios; ll n , m ; cin >> n >> m; rep(i , 1 , m) { ll x , y , z; cin >> x >> y >> z; a[x] += z; a[y] -= z; } rep(i , 1 , n) { if(a[i] > 0) in.pb(i); else if(a[i] < 0) ou.pb(i); } ll one = 0 , two = 0; while(true) { if(one == in.size() || two == ou.size()) break; ll ha = min(a[in[one]] , -a[ou[two]]); ans.pb(make_pair(make_pair(in[one] , ou[two]) , ha)); a[in[one]] -= ha; a[ou[two]] += ha; if(!a[in[one]]) one ++; if(!a[ou[two]]) two ++; } ll len = ans.size(); cout << len << '\n'; rep(i , 0 , len - 1) cout << ans[i].fi.fi << " " << ans[i].fi.se << " " << ans[i].se << "\n"; return 0; }
E. Spaceship Solitaire
题目链接:https://codeforces.ml/contest/1266/problem/E
题意:
你要建个飞船 , 建飞船需要 n 种资源 , 每种资源的需求量为 a[i] 。每一回合你只能选择一种资源并生产一个。然后你有里程牌???
里程碑包含三个元素 S 、 T 、 U , 意思是如果你有 T 个 S 资源, 你就可以获得一个U资源。(0 < T < a[S])
不同的里程碑 S 和 T 不相同 (如果S 相同且 T 相同则它们是同一个里程碑)
给你 q 次询问, 每次询问会加入一个里程碑(如果该里程碑先前存在过 , 则用现在的里程碑代替之前的里程碑 , 如果U == 0 , 则销毁该里程碑)
问建造飞船最少需要多少回合
分析:
i 种资源 每种需要 a[i] 个,那么当没有里程碑的情况下总的回合数为 a[1] + a[2] + ... + a[n] , 记为sum
首先我们要知道的是,不论如何,最后飞船是肯定可以建成的。又因为对任意里程碑 T < a[S] , 所以里程碑起作用的条件是肯定可以满足的 ,那么对于当前询问加入的里程碑来说
如果它先前出现过,则我们判断它先前出现的时候是否会对sum起影响(因为可能在该里程碑起作用之前,资源U已经采集的够了,所以该里程碑不起到作用。如果在起作用前U采集还不够,则它起到作用了, sum 会减一)
如果不影响,sum保持不变,影响了,则sum++(把先前造成的影响去掉)
然后我们判断新的里程碑是否起作用(如果起作用sum -- , 否则sum不变),对于每个询问输出sum即可。(只需判断当前类型里程碑,其他里程碑不受影响)
简单模拟题。
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 2e5 + 10; ll a[N] , sum; map<ll , ll> vis[N]; int main() { ios;cin.tie(0); int n , q; cin >> n ; rep(i , 1 , n) cin >> a[i] , sum += a[i]; cin >> q; rep(i , 1 , q) { ll s , t , u; cin >> s >> t >> u; if(vis[s][t]) { a[vis[s][t]] ++; if(a[vis[s][t]] > 0) sum ++; vis[s][t] = 0; } if(u) { vis[s][t] = u; a[u] --; if(a[u] >= 0) sum --; } cout << sum << '\n'; } return 0; }
加载全部内容